2
votes

I would like to compile my program but I have some problems to do so. My program is composed of a main function, written in C. The other files are either .h or .cpp files that I haven't written. I have a make file but unfortunately I cannot compile my program. This is the "architecture" of the files :

a.h only contains definitions of constant.

b.h and b.cpp. b.cpp includes a.h

c.h and d.cpp. c.h contains the signatures of d.cpp's methods. d.cpp includes a.h, b.h and c.h.

The last file is main.c which include c.h.

The makefile I used is the following one :

CXX      = g++
CXXFLAGS = -Wall -ansi -g
CC       = gcc
CCFLAGS  = -g
OBJS     = main.o a.o b.o c.o

exec : $(OBJS)
    $(CXX) -o $@ $(OBJS)

c.o: d.cpp b.h a.h  
    $(CXX) -c $(CXXFLAGS) $<

a.o: a.h
    $(CXX) -c $(CXXFLAGS) $<

b.o: b.cpp a.h
    $(CXX) -c $(CXXFLAGS) $<

%.o : %.cpp
    $(CXX) -c $(CXXFLAGS) $<

%.o : %.c
    $(CC) -c $(CCFLAGS) $<

Can someone me where I'm wrong ? Furthermore, how can I modify the makefile such that I can put a,b,c and d files in a subfolder without causing any problem ?

Thank you !

EDIT : This is the error I get :

g++ -o tester main.o a.o b.o c.o
Undefined symbols for architecture x86_64:
    "_crypto_aead_encrypt", referenced from:
         _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [tester] Error 1

The function is defined in declared in c.h and defined in d.cpp

1
Can you say what error do you have? - Piotr SmaroĊ„
Are you sure you are failing to compile, not to link? - MikeCAT
Yes the error happens during the link. I have added the error. - user1382272
Did you use extern "C" properly? - MikeCAT

1 Answers

2
votes

The most likely cause is that the definitions in c.h included by main is compiled as extern "C" when compiling main.c, but as C++ extern (name mangling) when compiled into d.cpp.

(if the c.h is the location where the crypto_aead_encrypt function comes from)

The easy solution would be to compile everything as C++ (either rename the main to .cpp, or compile main.c with ${CXX} and and add " -x c++" to CXXFLAGS).

The not-so-easy solution requires to compile the c.h methods as extern "C" when compiling d.cpp - this requires to change the c.h and/or d.cpp. Or provide a C wrapper for the C++ methods included from c.h.