如何改变这个makefile的结构
How to change the structure of this makefile
我是处理 makefile 的新手,我得到了这个文件:
demo: demo.cpp vegas.o ranlxd.o
g++ -o demo demo.cpp vegas.o ranlxd.o -lgfortran
vegas.o: vegas.F
gfortran -c vegas.F
ranlxd.o: ranlxd.c
gcc -c ranlxd.c
clean:
rm -f vegas.o ranlxd.o
distclean: clean
rm -f demo
我在演示文件中使用 vegas 和 ranlxd 文件中定义的函数。现在我想更改它,以便所有代码文件都先转换为目标文件,然后最后链接,所以我尝试了以下操作:
demo: demo.o vegas.o ranlxd.o
demo.o vegas.o ranlxd.o -lgfortran
demo.o: demo.cpp
g++ -c demo.cpp
vegas.o: vegas.F
gfortran -c vegas.F
ranlxd.o: ranlxd.c
gcc -c ranlxd.c
clean:
rm -f vegas.o ranlxd.o
distclean: clean
rm -f demo
这导致了错误
demo.o vegas.o ranlxd.o -lgfortran
make: demo.o: Command not found
make: *** [makefile:9: demo] Error 127
我做错了什么,我必须更改什么才能首先编译所有代码文件?我非常感谢任何形式的帮助!
用你的 link 命令你忘了实际为 linker
调用一个程序
demo: demo.o vegas.o ranlxd.o
demo.o vegas.o ranlxd.o -lgfortran
应该是:
demo: demo.o vegas.o ranlxd.o
g++ -o demo demo.o vegas.o ranlxd.o -lgfortran
Samuel 是对的,您只是忘记了 link 命令 (g++ -o demo
)。 Make 将 demo.o vegas.o ranlxd.o -lgfortran
传递给 shell,shell 抱怨说 demo.o
不是它知道的命令。
我添加这个答案只是为了让您了解 GNU make Built-In Rules. If you use GNU make you could also rely on them and replace your compilation and link rules by a single demo: demo.o vegas.o ranlxd.o
. Yes, just that. GNU make knows how to build the .o
files form your source files and how to assemble them to create the executable. Modify the standard variables,如果需要(例如 FC := gfortran
和 CC := gcc
)。您的 Makefile 可以很简单,例如:
FC := gfortran
CC := gcc
LDLIBS := -lgfortran
demo: demo.o vegas.o ranlxd.o
.PHONY: clean distclean
clean:
rm -f *.o
distclean: clean
rm -f demo
当然,如果您的目标是学习 make,那么您最好自己编写所有这些规则。
我是处理 makefile 的新手,我得到了这个文件:
demo: demo.cpp vegas.o ranlxd.o
g++ -o demo demo.cpp vegas.o ranlxd.o -lgfortran
vegas.o: vegas.F
gfortran -c vegas.F
ranlxd.o: ranlxd.c
gcc -c ranlxd.c
clean:
rm -f vegas.o ranlxd.o
distclean: clean
rm -f demo
我在演示文件中使用 vegas 和 ranlxd 文件中定义的函数。现在我想更改它,以便所有代码文件都先转换为目标文件,然后最后链接,所以我尝试了以下操作:
demo: demo.o vegas.o ranlxd.o
demo.o vegas.o ranlxd.o -lgfortran
demo.o: demo.cpp
g++ -c demo.cpp
vegas.o: vegas.F
gfortran -c vegas.F
ranlxd.o: ranlxd.c
gcc -c ranlxd.c
clean:
rm -f vegas.o ranlxd.o
distclean: clean
rm -f demo
这导致了错误
demo.o vegas.o ranlxd.o -lgfortran
make: demo.o: Command not found
make: *** [makefile:9: demo] Error 127
我做错了什么,我必须更改什么才能首先编译所有代码文件?我非常感谢任何形式的帮助!
用你的 link 命令你忘了实际为 linker
调用一个程序demo: demo.o vegas.o ranlxd.o
demo.o vegas.o ranlxd.o -lgfortran
应该是:
demo: demo.o vegas.o ranlxd.o
g++ -o demo demo.o vegas.o ranlxd.o -lgfortran
Samuel 是对的,您只是忘记了 link 命令 (g++ -o demo
)。 Make 将 demo.o vegas.o ranlxd.o -lgfortran
传递给 shell,shell 抱怨说 demo.o
不是它知道的命令。
我添加这个答案只是为了让您了解 GNU make Built-In Rules. If you use GNU make you could also rely on them and replace your compilation and link rules by a single demo: demo.o vegas.o ranlxd.o
. Yes, just that. GNU make knows how to build the .o
files form your source files and how to assemble them to create the executable. Modify the standard variables,如果需要(例如 FC := gfortran
和 CC := gcc
)。您的 Makefile 可以很简单,例如:
FC := gfortran
CC := gcc
LDLIBS := -lgfortran
demo: demo.o vegas.o ranlxd.o
.PHONY: clean distclean
clean:
rm -f *.o
distclean: clean
rm -f demo
当然,如果您的目标是学习 make,那么您最好自己编写所有这些规则。