我在使用 GNU g++ 编译多个文件时遇到问题,所以请帮助我。
我们假设有五个文件:
main.cpp : the main function
a.h : the header file of class A
a.cpp : the definition of class A
b.h : the header file of class B
b.cpp : the definition of class B
程序中,main.cpp
使用A类(因此包含a.h
),A类使用B类(因此包含b.h
) .
那么,如何编译这个程序来生成执行文件呢?
我尝试使用下面的命令,却反馈错误:
undefined reference to ~(objects in class A and B)".
command: g++ -Wall -O2 -s main.cpp
我的操作系统是ubuntu 12.04。我知道如果我用像 dev c++ 这样的东西创建一个项目,它会自动将文件链接在一起,但我不想使用它。我也看过其他链接和来源,但找不到对我自己有用的东西。
最佳答案
尝试
g++ -Wall -O2 main.cpp a.cpp b.cpp
这将创建一个程序 a.out
,它是可执行文件。 -Wall
标志只是告诉编译器发出更多警告,而 -O2
标志启用优化。最后三个参数是您的源文件。您需要以任何方式将所有不是 #include
:d 的源文件提供给 g++
。
您可以使用选项 -o (name)
指定可执行文件的名称。
关于c++ - 在 g++ 中编译头文件和 cpp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22308867/