g ++中的目标文件是否有等效的-I(大写字母I)?

Is there an equivalent of -I (capital I) for object files in g++?

我有几个来自不同目录的目标文件(它们存储在生成它们的相应源附近)。有没有办法给出这个目录结构

根目录

main.o

Root/Some_long_path

object_1.o
object_2.o

我可以运行这样的命令

g++ -Wall main.o -ISome_long_path object_1.o object_2.o -o app

这样我就不必将完整路径放在每个目标文件的前面。用什么代替 -I 命令?

我正在使用 gcc 版本 4.8.3(来自 Cygwin 安装)。

So that I don't have to put the full path in front of every object file. What would go instead of the -I command?

没有像-I 那样的gcc 选项来向对象"search path" 添加一些目录,也没有对象的搜索路径。但是有库的搜索路径(在 Unix 世界中,静态库通常命名为 lib*.a,共享库通常命名为 lib*.so)。

gcc 的目录选项手册仅列出包含路径的 -I 选项和库路径的 -L 选项。没有对象路径:

https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#Directory-Options

和Link-gcc的选项手册仅提及-L选项(靠近-l描述):

https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-Options

你能用什么:

  • shell 变量或环境变量(在 bash 中:OBJ_DIR=./path/to/dir 然后 g++ ... $OBJ_DIR/obj1.o $OBJ_DIR/obj2.o
  • 将多个对象从单个目录打包到单个 static library libSome_Long_Component_Name.a(这种类型的库就像几个 *.o 对象文件的存档和一些用 ar rcs);那么你可以使用 g++ ... -Ldir/ -lSome_Long_Component_Name
  • Makefiles 和 make 实用程序的一些变体(gnu make 用于 linux 和 cygwin,nmake 用于 windows 的 MSVC;您可以从 gnu make 手册开始:http://www.gnu.org/software/make/manual/make.html#Simple-Makefile then "2.4 Variables Make Makefiles Simpler" then "4.3 Types of Prerequisites" - with example of addprefix command to ask make find objects in some objdir. There is also VPATH special variable 指示 make 在多个目录中搜索源和对象;但项目中的所有对象都应具有不同的名称)。
  • 一些 IDE 支持 Unix 项目 (Code::Blocks, list1, list2, wikilist of C/C++ IDE?);他们通常管理您项目的所有资源,并能够为项目生成 Makefile。
  • 一些更现代的构建系统,而不是 make:例如 CMake, SCons(如果您使用 Qt,则为 qmake)。