I'm trying to make a makefile with multiple files. Can someone help me? The files I have are file1.cpp, file2.h and main.cpp
我正在尝试使用多个文件制作一个makefile。有人能帮我吗?我拥有的文件是file1.cpp,file2.h和main.cpp
file1.cpp contains my functions. file2.h contains the declaration of my functions.
file1.cpp包含我的函数。 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.
但是当我尝试编译时,它给了我很多错误。我的代码在日食上完美无缺。
2 个解决方案
#1
6
you'll need to compile all .cpp files that you use (i assume they are not included somewhere). That way the compiler will know that file1.cpp and main.cpp are used together. Also I would suggest using g++ instead of gcc, because g++ is the specific c++ compiler while gcc supports C and C++.
你需要编译你使用的所有.cpp文件(我假设它们不包括在某处)。这样编译器就会知道file1.cpp和main.cpp是一起使用的。另外我建议使用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 :)
希望这可以帮助 :)
#2
1
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. Use g++
instead.
gcc不是C ++编译器。请改用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
#1
6
you'll need to compile all .cpp files that you use (i assume they are not included somewhere). That way the compiler will know that file1.cpp and main.cpp are used together. Also I would suggest using g++ instead of gcc, because g++ is the specific c++ compiler while gcc supports C and C++.
你需要编译你使用的所有.cpp文件(我假设它们不包括在某处)。这样编译器就会知道file1.cpp和main.cpp是一起使用的。另外我建议使用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 :)
希望这可以帮助 :)
#2
1
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. Use g++
instead.
gcc不是C ++编译器。请改用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