编写makefile来构建动态库
我的 src 目录中有一个 makefile。 makefile 应该构建位于 DataStructures/ 中的数据结构,然后迭代calculations/ 中的所有 cpp 文件并在 ../bin/calculations 中创建相应的 .so 文件
我尝试了以下语法:
DAST = DataStructures/
COMPS = computations/
BIN = ../bin/
OBJECTS = ${DAST}Atom.o ${DAST}Molecule.o
COMPILE = g++ -Wall -g -c -std=c++0x -I/usr/local/include/openbabel-2.0 LINK = g++ -Wall -g -std=c++0x ${OBJECTS} -lopenbabel -I/usr/local/include/openbabel-2.0
all: ${BIN}main ${DAST}Molecule.o ${DAST}Atom.o ${BIN}${COMPS}%.so
${BIN}main: ${OBJECTS} main.cpp
${LINK} main.cpp -o ${BIN}main
${DAST}Molecule.o: ${DAST}Molecule.h ${DAST}Molecule.cpp
${COMPILE} ${DAST}Molecule.cpp -o ${DAST}Molecule.o
${DAST}Atom.o: ${DAST}Atom.h ${DAST}Atom.cpp
${COMPILE} ${DAST}Atom.cpp -o ${DAST}Atom.o
${BIN}${COMPS}%.o: ${COMPS}%.cpp
gcc -Wall -fPIC -c -lopenbabel $< -I/usr/local/include/openbabel-2.0 -std=c++0x
${BIN}${COMPS}%.so: ${COMPS}%.o
gcc -shared -Wl,-soname,libcsmtest.so.1 -o libcsmtest.so $@
clean:
rm -rf ${OBJECTS}
.PHONY: all clean
但它显然没有工作,因为我得到以下输出:
shai@ubuntu:~/csm/csm2/src$ make all
make: *** No rule to make target `../bin/computations/%.so', needed by 'all'. Stop.
谢谢
I have a makefile in my src directory.
The makefile should build the data structures, which are in DataStructures/, and then iterate over all cpp files in calculations/ and create a corresponding .so file in ../bin/calculations
I tried the following syntax:
DAST = DataStructures/
COMPS = computations/
BIN = ../bin/
OBJECTS = ${DAST}Atom.o ${DAST}Molecule.o
COMPILE = g++ -Wall -g -c -std=c++0x -I/usr/local/include/openbabel-2.0 LINK = g++ -Wall -g -std=c++0x ${OBJECTS} -lopenbabel -I/usr/local/include/openbabel-2.0
all: ${BIN}main ${DAST}Molecule.o ${DAST}Atom.o ${BIN}${COMPS}%.so
${BIN}main: ${OBJECTS} main.cpp
${LINK} main.cpp -o ${BIN}main
${DAST}Molecule.o: ${DAST}Molecule.h ${DAST}Molecule.cpp
${COMPILE} ${DAST}Molecule.cpp -o ${DAST}Molecule.o
${DAST}Atom.o: ${DAST}Atom.h ${DAST}Atom.cpp
${COMPILE} ${DAST}Atom.cpp -o ${DAST}Atom.o
${BIN}${COMPS}%.o: ${COMPS}%.cpp
gcc -Wall -fPIC -c -lopenbabel lt; -I/usr/local/include/openbabel-2.0 -std=c++0x
${BIN}${COMPS}%.so: ${COMPS}%.o
gcc -shared -Wl,-soname,libcsmtest.so.1 -o libcsmtest.so $@
clean:
rm -rf ${OBJECTS}
.PHONY: all clean
But it obviously doesn't work, as I get the following output:
shai@ubuntu:~/csm/csm2/src$ make all
make: *** No rule to make target `../bin/computations/%.so', needed by 'all'. Stop.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在
all:
目标中明确指定先决条件。在 Makefile 中,% 是一个通配符,可以在自动规则中使用。但是,
all:
目标是一个没有此类通配符的简单目标,因此${BIN}${COMPS}%.so
在该上下文中是错误的。请注意,当我在这种情况下说“通配符”时,该通配符将目标与先决条件进行匹配,而不是像 glob 表达式中的
*
那样与文件系统匹配。另外,虽然你的心处于正确的位置,但从风格上来说,你的 Makefile 可以更好:
all
目标的先决条件,而只是你希望的最终目标船。%.so
编写自动规则,因为库通常是由多个对象构建的。*.cpp
(或.c
)以及 < 包含的所有标头(直接和间接)代码>*.cpp 文件。${COMPILE}
,而应该为您的 C++ 编译器使用$(CXX)
,并且 < code>$(CXXFLAGS) 表示您希望传递给该编译器的标准标志。You need to specify in the
all:
target, the prerequisites explicitly.In Makefile parlance, % is a wildcard that can be used in automatic rules. However, the
all:
target is a simple target with no such wildcard, thus${BIN}${COMPS}%.so
is wrong in that context.Please note that when I say 'wildcard' in this context, this wildcard matches the target against the prerequisites, not against the filesystem like
*
do in glob expressions.Also, while your hart is in the right place, as a matter of style, your Makefile can be better:
all
target, but only the final targets you wish to ship.%.so
, because a library is often constructed from more than one object.*.cpp
(or.c
) as well as all the headers included (directly and indirectly) by the*.cpp
file.${COMPILE}
as you do, one should use$(CXX)
for your C++ compiler, and$(CXXFLAGS)
for the standard flags you wish to pass to that compiler.您需要类似的东西
您需要一种方法来收集变量
SOBJECTS
中的所有 *.so 名称。您可以手动执行此操作,或使用make
的一些内部函数来扫描源目录。另请注意,我从
all
目标中删除了两个作为依赖项的*.o
文件。它们不是构建的最终目标(我认为),因此您不需要在那里提及它们。除此之外,还有其他风格点,我会采取不同的做法,但目前它们不会造成直接问题,所以我不会离题,但我建议您看一些教程,了解一般情况下是如何完成的。
对于初学者,请查看 Paul 的 Makefile 规则 和 如何不使用 VPATH。
You need something like
You need a way to gather all the *.so names in the variable
SOBJECTS
. You can do this manually, or use some ofmake
's internal functions to scan the source directory.Also notice that I removed the two
*.o
files as dependencies from theall
target. They are not final goals of the build (I assume), so you don't need to mention them there.Besides this there are other stylistic points which I would do differently, but at the moment they are not causing immediate problems, so I won't digress, but I advise you to have a look at some tutorials to see how things are done generally.
For starters, look at Paul's Rules of Makefiles, and How Not to Use VPATH.