Reputation: 2176
I have a c
project which I have been building using the following Makefile
.
CC=gcc
CFLAGS= -g
LIBS= -lm
MOSEK_H= /home//work/mosek/6/tools/platform/linux32x86/h/
MSKLINKFLAGS= -lmoseknoomp -lpthread -lm
MSKLIBPATH= /home/work/mosek/6/tools/platform/linux32x86/bin/
all: learn
clean: tidy
rm -f learn
tidy:
rm -f *.o
learn: spl.o api.o mosek_qp_optimize.o
$(CC) $(CCFLAGS) spl.o api.o -o learn \
$(LIBS) -L $(MSKLIBPATH) $(MSKLINKFLAGS)
spl.o: spl.c
$(CC) -std=c99 -c $(CFLAGS) spl.c -o spl.o
mosek_qp_optimize.o: mosek_qp_optimize.c
$(CC) -c $(CFLAGS) mosek_qp_optimize.c -o mosek_qp_optimize.o -I $(MOSEK_H)
api.o: api.c api_types.h
$(CC) -c $(CFLAGS) api.c -o api.o
The third party c++ code I need to use comprises of .cpp
files (graph.cpp
, maxflow.cpp
, test.cpp
) and header files(graph.h
, block.h
). Independently I can compile the c++ project using the following and it seems to work fine.
g++ test.cpp graph.cpp maxflow.cpp
Now I need to move the code in the main()
of test.cpp
into api.c
of the original c
project. Therefore, I need to compile api.c
using g++
.
I tried the following but it doesn't seem to work:
CC=gcc
CFLAGS= -g
LIBS= -lm
MOSEK_H= /home//work/mosek/6/tools/platform/linux32x86/h/
MSKLINKFLAGS= -lmoseknoomp -lpthread -lm
MSKLIBPATH= /home/work/mosek/6/tools/platform/linux32x86/bin/
MAXFLOW_H= /home/work/maxflow/
all: learn
clean: tidy
rm -f learn
tidy:
rm -f *.o
learn: spl.o api.o mosek_qp_optimize.o graph.o maxflow.o
$(CC) $(CCFLAGS) spl.o api.o mosek_qp_optimize.o graph.o maxflow.o -o learn \
$(LIBS) -L $(MSKLIBPATH) $(MSKLINKFLAGS)
spl.o: spl.c
$(CC) -c $(CFLAGS) spl.c -o spl.o
mosek_qp_optimize.o: mosek_qp_optimize.c
$(CC) -c $(CFLAGS) mosek_qp_optimize.c -o mosek_qp_optimize.o -I $(MOSEK_H)
api.o: api.cpp api_types.h
$(CC) -c $(CFLAGS) api.cpp -o api.o -I $(MAXFLOW_H)
graph.o: graph.cpp
$(CC) -c $(CFLAGS) graph.cpp -o graph.o -I $(MAXFLOW_H)
maxflow.o: maxflow.cpp
$(CC) -c $(CFLAGS) maxflow.cpp -o maxflow.o -I $(MAXFLOW_H)
Any ideas, how to go about this?
Upvotes: 0
Views: 306
Reputation: 93446
g++ and gcc differ only in what default libraries are linked; g++ will link stdlibc++ as well as libc and libm. Otherwise they are the same compiler; they both support C and C++ compilation. By default any file with a .c extension will be compiled as C code even if g++ is used - it does not gain C++ linkage by virtue of using g++.
Your assertion:
Now I need to move the code in the main() of test.cpp into api.c of the original c project. Therefore, I need to compile api.c using g++.
does not follow; api.c will be compiled by the C compiler, and any C++ code it contains or references to code with C++ linkage will fail. C++ compiled code called from C code must have C linkage. If api.c contains C++ code, you will have to compile it as C++ code (rename it api.cpp or force by compiler switch).
Upvotes: 1