[英]How To G++ compile A .cpp File With Dependencies In Another Directory
My project is organized the following way: 我的项目通过以下方式组织:
mainfolder
|
|_main.cpp
|
|_subfolder
|
|_foo.hpp
|_foo.cpp
|_bar.hpp
|_bar.cpp
main.cpp uses functions and variables from foo.hpp and bar.hpp, whose function implementations are in foo.cpp and bar.cpp respectively main.cpp使用foo.hpp和bar.hpp中的函数和变量,它们的函数实现分别在foo.cpp和bar.cpp中
I was wondering how I would use g++ to compile main.cpp 我想知道如何使用g ++编译main.cpp
Thanks :) 谢谢 :)
You can compile main on its own like this: 您可以像这样自己编译main:
g++ -c main.cpp -Isubfolder -o main.o
This line compiles main.cpp into an object file (not an application) but it gives it the path ( -I<paht>
) where it can fine the include files 该行将main.cpp编译为目标文件(不是应用程序),但为它提供了路径(
-I<paht>
),可以在其中优化包含文件
You probably want to then also compile the other two files into objects: 然后,您可能还希望将其他两个文件编译为对象:
g++ -c subfolder/foo.cpp -Isubfolder -o foo.o
g++ -c subfolder/bar.cpp -Isubfolder -o bar.o
Where the -c
flag means compile only (and not link). -c
标志表示仅编译(而非链接)。 So then you probably want to link your objects together: 因此,您可能想要将对象链接在一起:
g++ main.o foo.o bar.o -o myapp
Or in one line: 或一行:
g++ main.cpp subfolder/foo.cpp subfolder/bar.cpp -Isubfolder -o myapp
The first method (to do it in two steps) is generally easier when you want to create a pattern rule to compile files (with gnu make), the second (in one line) is easier to do manually 当您要创建模式规则来编译文件(使用gnu make)时,第一种方法(分两步执行)通常更容易,第二种方法(一行)则更容易手动完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.