如何使用命令行在C++中编译和链接分隔的 .h 和 .cpp 文件

How to compile and link separated .h and .cpp files in C++ using the command line?

本文关键字:分隔 链接 文件 cpp 编译 命令行 何使用 C++      更新时间:2023-10-16

我想使用文件夹分隔函数定义和函数标头,但出现致命错误,指出找不到"x.h"。下面提供的文件名只是示例。这可能吗?

这是我正在使用的行:

g++ -I ./headers/ -o main2.o main2.cpp ./definitions/x.cpp 

如果您确定在正确的路径中有正确的文件,一个问题是:-

-I后不应该有任何空间,只使用./headers

g++ -I./headers -o main2.o main2.cpp ./definitions/x.cpp 
      ^^No Space