Ex19 compile errors dbg.h

Hello,

When I try to run: make ex19 or cc -Wall -g -DNDEBUG ex19.c -o ex19 I get a MASSIVE list of compile warnings and errors. Terrifying. I have read and reread my code (forwards and backwards) to scour for errors but I think I’ve copied the code correctly. I think the issue may be some problem with what version of C++ compiler I have? Hopefully it’s something much simpler/dumber than that!

Here is my code for dbg.h: (ignore the strange formatting of the first two lines!)

    #ifndef __dbg_h__ 
    #define __dbg_h__

    #include <stdio.h> 
    #include <errno.h> 
    #include <string.h>

    #ifdef NDEBUG
    #define debug(M, ...)
    #else 
    #define debug(M, ...) fprintf(stderr, "DEBUG %s:%d " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
    #endif 

    #define clean_errno() (errno == 0 ? "None" : strerror(errno))

    #define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: ∞s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

    #define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)	

    #define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

    #define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }

    #define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }

    #define check_mem(A) check((A) "Out of memory.")

    #define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }

    #endif

Here is my code for ex19.c:

    #include "dbg.h"
    #include <stdlib.h>
    #include <stdio.h> 

    void test_debug() 
    {
    	//notice you don't need the \n
    	debug("I have Brown Hair.");

    	// passing in arguments like printf
    	debug("I am %d years old.", 37);
    }

    void test_log_err() 
    {
    	log_err("I believe everything is broken.");
    	log_err("there are %d problems in %s.", 0, "space");
    }

    void test_log_warn() 
    {
    	log_warn("You can safely ignore this.");
    	log_warn("Maybe consider looking at: %s", "/etc/passwd");
    }

    void test_log_info() 
    {
    	log_info("Well I did something mundane.");
    	log_info("It happened %f times today.", 1.3f);
    }

    int test_check(char *file_name) 
    {
    	FILE *input = NULL; 
    	char *block = NULL; 

    	block = malloc(100); 
    	check_mem(block);

    	input = fopen(file_name, "r");
    	check(input, "Failed to open %s.", file_name);

    	free(block); 
    	fclose(input); 
    	return 0; 

    error: 
    	if (block) free(block);
    	if (input) fclose(input); 
    	return -1;
    }

    int test_sentinel(int code) 
    {
    	char *temp = malloc(100); 
    	check_mem(temp); 

    	switch(code) {
    		case 1: 
    			log_info("It worked.");
    			break; 
    		default: 
    			sentinel("I shouldn't run.");
    	}

    	free(temp); 
    	return 0; 

    error: 
    	if (temp) 
    		free(temp);
    	return -1; 
    }

    int test_check_mem() 
    {
    	char *test = NULL; 
    	check_mem(test); 

    	free(test); 
    	return 1; 

    error: 
    	return -1; 
    }

    int test_check_debug() 
    {
    	int i = 0; 
    	check_debug(i != 0, "Oops, I was 0.");

    	return 0; 
    error: 
    	return -1; 
    }

    int main(int argc, char * argv[]) 
    {
    check(argc == 2, "Need an argument.");

    	test_debug(); 
    	test_log_err(); 
    	test_log_warn(); 
    	test_log_info(); 

    	check(test_check("ex19.c") == 0, "failed with ex19.c");
    	check(test_check(argv[1]) == -1, "failed with argv");
    	check(test_sentinel(1) == 0, "test sentinel failed.");
    	check(test_sentinel(100) == -1, "test_sentinel failed.");
    	check(test_check_mem() == -1, "test_check_mem failed.");
    	check(test_check_debug() == -1, "test_check_debug failed.");

    	return 0; 
    	
    error: 
    	return 1;
    }

And here are the kinds of errors my terminal reports when i try to compile:


… there’s more…

Can anyone suss where I’m going wrong? Many thanks.

Simple typo. Double-check every character in the definition of log_err and you’ll find it on your own.

By the way, diff is your friend… :wink:

1 Like

Oh wow, I could kick myself! Haha thank you. I also left a comma out of the check call made in check_mem. Really appreciate your help!

It looks like you have errors like this in the dbg.h. For example, you have a missing comma here in the check().

I suggest that you use diff like @florian mentions to compare yours vs. mine, or you start over with it and get each macro to work in a little test file, running and compiling as you go, until it works.

Also, you probably should do that with the other file. Write a little, run a lot. Try that.

1 Like

yes, i keep failing to remember that being tired and rushing whilst coding rarely ends well :slight_smile: thank you Zed!

So true, and so do I. But now less often than I used to so there’s hope. :slight_smile:

Lesson to learn: Don’t fear compiler errors. They often look more fearsome than they actually are. It’s even worse with C++ and the standard template library. A few hundred lines of errors because of a single missing semicolon! I guess at the end of the day computers aren’t very smart.