简体   繁体   English

在 C++ 生成文件中包含 C 文件

[英]Include C files in C++ makefile

I am coding in c++ and had a perfectly working makefile for that.我正在用 C++ 编码,并且为此有一个完美的 makefile。 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.原来的代码可用混合 C 文件以及 C++ 文件。 When, trying to compile now I have this Undefined symbols for architecture x86_64 error.当,现在尝试编译时,我遇到了架构 x86_64 错误的未定义符号。 And I am pretty sure it is because I did not adapt my makefile correctly.我很确定这是因为我没有正确地调整我的 makefile。 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.可能如果有人看一下这个makefile,我猜会更容易。 Also I have noticed that none of the .o files are made for my .c files but they are made for the .cpp另外我注意到没有一个 .o 文件是为我的 .c 文件制作的,但它们是为 .cpp 制作的

Hope someone can help.希望有人能帮忙。 I've been stuck on it for 3 days我已经坚持了 3 天

##### 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)

You just don't have any dependencies on the C object files.您只是对 C 对象文件没有任何依赖关系。 You need to add them.您需要添加它们。 You have this for the CPP ones:你有这个给 CPP 的:

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 .目前,您的每个%.o文件都取决于%.cpp%.c You'll want to separate them somehow, otherwise you'll just get errors regarding no rule to make the other targets.你会想以某种方式将它们分开,否则你只会得到关于没有规则来制作其他目标的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM