ChrisTremblay
ChrisTremblay

Reputation: 9

Include C files in C++ makefile

I am coding in c++ and had a perfectly working makefile for that. Then, for a project purpose, I had to go online and find code to do what I wanted to do. Turns out the code available mix C files as well as C++ files. When, trying to compile now I have this Undefined symbols for architecture x86_64 error. And I am pretty sure it is because I did not adapt my makefile correctly. I've been struggling searching everywhere trying to find answers but nothing ever works for me. May be if someone take a look at this makefile, it would be easier I guess. Also I have noticed that none of the .o files are made for my .c files but they are made for the .cpp

Hope someone can help. I've been stuck on it for 3 days

##### Configurable options:

# ---------------------------------------------------------------------
# Compiler section 
# ---------------------------------------------------------------------

## Compiler:
CC=gcc
#CC=cc
CPP=g++

## Compiler flags:

# GCC:  (also -march=pentium etc, for machine-dependent optimizing)
CFLAGS=-Wall -O3 -fomit-frame-pointer -funroll-loops
LDFLAGS=-L/usr/local/lib
LIBS=-lopenbabel 
CPPFLAGS=$(CFLAGS) -I Project/Lib/openbabel-2.0/ -I ProjectCode/Include/ -I ProjectCode/Lib/boost_1_60_0/
CXXFLAGS=-Wall -g -std=c++11 -I.

# GCC w/ debugging:
#CFLAGS=-Wall -g -DINLINE

# Compaq C / Digital C:
#CFLAGS=-arch=host -fast

# SunOS:
#CFLAGS=-fast

## Program options:

# Enable long options for cl (eg. "cl --help"), comment out to disable.
# Requires getopt_long(3)  (a GNU extension)
LONGOPTS = -DENABLE_LONG_OPTIONS


# ---------------------------------------------------------------------
# Directories section 
# ---------------------------------------------------------------------


OBJFILES = ProjectCode/Objects/Project.o ProjectCode/Objects/stdafx.o ProjectCode/Objects/BoxTools.o ProjectCode/Objects/TanimotoStruct.o
COBJFILES = ProjectCode/Objects/cliquer.o ProjectCode/Objects/graph.o ProjectCode/Objects/reorder.o

EXEC = Project



# ---------------------------------------------------------------------
# Compilation section 
# ---------------------------------------------------------------------

ProjectCode/Objects/%.o: ProjectCode/Source/%.c
$(CC) -o $@ -c $< $(CFLAGS) $(LIBS)
ProjectCode/Objects/%.o: ProjectCode/Source/%.cpp
$(CPP) $(CPPFLAGS) -o $@ -c $<
Project: $(OBJFILES)
$(CPP) $(LDFLAGS) -o $@ $^ $(CXXFLAGS) $(LIBS);


# ---------------------------------------------------------------------
# Cleaning section 
# ---------------------------------------------------------------------

clean:
     rm -f ProjectCode/Objects/*.o $(EXEC)

Upvotes: 1

Views: 1903

Answers (1)

Barry
Barry

Reputation: 302817

You just don't have any dependencies on the C object files. You need to add them. You have this for the CPP ones:

Project: $(OBJFILES)
    $(CPP) $(LDFLAGS) -o $@ $^ $(CXXFLAGS) $(LIBS);

But it probably should look like:

Project: $(OBJFILES) $(COBJFILES)
    $(CPP) $(LDFLAGS) -o $@ $^ $(CXXFLAGS) $(LIBS);

Also at the moment each of your %.o files depends on both a %.cpp and a %.c. You'll want to separate them somehow, otherwise you'll just get errors regarding no rule to make the other targets.

Upvotes: 1

Related Questions