c++ - 在 g++ 中编译头文件和 cpp 文件

标签 c++ header compiler-errors g++ header-files

我在使用 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/

相关文章:

c++ - 使用动态菜单的命令范围

security - 如何在 Amazon S3 上设置 "X-Content-Type-Options : nosniff"?

java - 从技术上讲,您可以在 main 方法中调用 string[] 吗?

types - 函数的 Rust HashMap 的类型签名

c# - 想做一个 php get_include_files() 增强

c++ - C++ 按字母顺序嵌套数据结构

javascript - 定时器从 h1 传递到 h6

c++ - 静态数据成员的C++类内初始化

generics - TypeScript 泛型类 : Cannot assign to Generic Type Variable

c++ - 如何从 WIC 解码器确定自上而下/自下而上?