-1

So I'm doing my final project and am trying to incorporate some of the functions from dictionary.c into my program. I have included #include "dictionary.h" at the top of my file as was done in speller.c before, as well as included dictionary.h dictionary.c and dictionary.o files to the same folder but I am getting the following error:

jharvard@appliance (~/Dropbox/project): make encrypt
clang -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror    encrypt.c  -lcs50 -lm -o encrypt
/tmp/encrypt-5da335.o: In function `main':
/home/jharvard/Dropbox/project/encrypt.c:103: undefined reference to `load'
/home/jharvard/Dropbox/project/encrypt.c:151: undefined reference to `check'
/home/jharvard/Dropbox/project/encrypt.c:166: undefined reference to `unload'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [encrypt] Error 1

Any ideas? Thanks!


Update: So after changing it to include "dictionary.c" I get errors like this:

/home/jharvard/Dropbox/project/dictionary.c:34: multiple definition of `root'
encrypt.o:/home/jharvard/Dropbox/project/./dictionary.c:34: first defined here 
dictionary.o: In function `size':

Here is my makefile:

# # compiler to use
# CC = clang
# # flags to pass compiler
# CFLAGS = -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror -lm
# # name for executable
# EXE = encrypt
# # space-separated list of header files
# HDRS = dictionary.h
# # space-separated list of libraries, if any,
# # each of which should be prefixed with -l
# LIBS = -lcs50
# # space-separated list of source files
# SRCS = encrypt.c dictionary.c
# # automatically generated list of object files
# OBJS = $(SRCS:.c=.o)
# # default target
# $(EXE): $(OBJS) $(HDRS) Makefile
#   $(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBS)
# # dependencies 
# $(OBJS): $(HDRS) Makefile
# # housekeeping
# clean:
#   rm -f core $(EXE) *.o
1
2

That is a linker error, not a compiler error. Your compiler won't complain, because it doesn't know of dictionary.c's existence. Including dictionary.c in your headers doesn't make any sense at all, because that's the whole point of including dictionary.h.

That is a basic concept: You include the header file, which has the relevant declarations for the functions used in your code, so that the compiler knows that those files are to be found somewhere. The key word here is "somewhere": The compiler doesn't know, nor care about how those functions are defined. It just cares about what types the functions take as arguments and what they're meant to return, and it makes sure the types are correct. As long as that's fine (meaning, as long as the function is called correctly), the compiler is going to create the .o file for your .c file. One .o file for each .c file in your project Then, the linker will grab all the .o files, and it is the linker's job to put everything together.

What you're not telling your compiler (and therefore the linker) is that you're trying to compile the code for dictionary.c as well. So, there is an encrypt.o file, but not a dictionary.o file, so when the linker tries to implement those functions you called, it can't cause they don't exist.

Go back to speller.c, run make and look at what clang is doing there. What .c files are included in the compilation process? Is only speller.c (which would be equivalent to encrypt.c in your program) included? Or is dictionary.c also included there?

You need to tell clang to compile dictionary.c as well as encrypt.c.

3
  • The original post shows clang not including dictionary.c as source (i don't know if you edited the makefile before or after, but the original clang command showed it wasn't being used) The current makefile looks correct, so the new error is due to including the dictionary.c file as a header.
    – Irene
    Nov 21 '15 at 3:19
  • 1
    Please, ronga, stop giving wrong advice to people! dictionary.c should NOT be included in the HDRS field of the makefile. A .c file should NOT be included as a header. Ever. Please read about the compilation process
    – Irene
    Nov 21 '15 at 3:40
  • Thank you for the help!
    – GCOTT94
    Nov 21 '15 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .