简体   繁体   English

Linux上的C ++ makefile,带有多个* .cpp文件

[英]C++ makefile on Linux with Multiple *.cpp files

I'm trying to make a makefile with multiple files. 我正在尝试使用多个文件制作一个makefile。 Can someone help me? 有人能帮我吗? The files I have are file1.cpp, file2.h and main.cpp 我拥有的文件是file1.cpp,file2.h和main.cpp

file1.cpp contains my functions. file1.cpp包含我的函数。 file2.h contains the declaration of my functions. file2.h包含我的函数声明。

main.cpp [includes file2.h in the code] file1.cpp [includes file2.h in the code] main.cpp [包含代码中的file2.h] file1.cpp [包含代码中的file2.h]

i did 我做到了

all: main
gcc -g -Wall -o main main.cpp

but it gives me tons of bugs when i try to compile. 但是当我尝试编译时,它给了我很多错误。 my codes works perfectly fine on eclipse. 我的代码在日食上运行得很好。

you'll need to compile all .cpp files that you use (i assume they are not included somewhere). 你需要编译你使用的所有.cpp文件(我假设它们不包括在某处)。 That way the compiler will know that file1.cpp and main.cpp are used together. 这样编译器就会知道file1.cpp和main.cpp是一起使用的。 Also I would suggest using g++ instead of gcc, because g++ is the specific c++ compiler while gcc supports C and C++. 另外我建议使用g ++而不是gcc,因为g ++是特定的c ++编译器,而gcc支持C和C ++。

Try using: 尝试使用:

g++ -g -Wall -o main main.cpp file1.cpp

Also I would recommend to use Makefile variables like this: 另外我建议使用像这样的Makefile变量:

SOURCES = main.cpp file1.cpp
g++ -g -Wall -o main $(SOURCES)

Hope this helps :) 希望这可以帮助 :)

but it gives me tons of bugs when i try to compile. 但是当我尝试编译时,它给了我很多错误。 my codes works perfectly fine on eclipse. 我的代码在日食上运行得很好。

gcc is not a C++ compiler. gcc不是C ++编译器。 Use g++ instead. 请改用g++

Your Makefile should look like so, it can leverage implicit rules : 你的Makefile应该是这样的,它可以利用隐式规则

all: main

CXXFLAGS+=-g -Wall
LDLIBS+=-lstdc++
main: file1.o main.o

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

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