在Linux上使用多个* .cpp文件的C ++ makefile [英] C++ makefile on Linux with Multiple *.cpp files

查看:283
本文介绍了在Linux上使用多个* .cpp文件的C ++ makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个有多个文件的makefile。有人可以帮助我吗?
我有的文件是file1.cpp,file2.h和main.cpp

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

file1.cpp包含我的函数。
file2.h包含我的函数的声明。

file1.cpp contains my functions. file2.h contains the declaration of my functions.

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

main.cpp [includes file2.h in the code] file1.cpp [includes file2.h in the code]

我做了

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

的bug,当我尝试编译。我的代码在eclipse上工作得很好。

but it gives me tons of bugs when i try to compile. my codes works perfectly fine on eclipse.

推荐答案

你需要编译所有的.cpp文件不包括在某处)。
这样编译器会知道file1.cpp和main.cpp是一起使用的。
我建议使用g ++代替gcc,因为g ++是特定的c ++编译器,而gcc支持C和C ++。

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++.

尝试使用:

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

此外,我建议使用如下的Makefile变量:

Also I would recommend to use Makefile variables like this:

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

希望这有助于:)

这篇关于在Linux上使用多个* .cpp文件的C ++ makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆