g ++编译另一个目录中存在的源文件 [英] g++ compile source files existing in another directory

查看:115
本文介绍了g ++编译另一个目录中存在的源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用makefile建立一个构建过程,用于我正在开发的C ++插件。我很想知道是否有任何方法可以让g ++在另一个目录中找到 source 文件。我这样做的动机是为了避免为每个源文件指定相对路径,如下所述。



我的项目目录结构如下所示:

  MyPlugin  - > src  - > Foo.cpp 
- > Foo.h
- > Bar.cpp
- > Bar.cpp
- >构建 - > Makefile

以下是我目前Makefile的精简版:

  SRC_PATH = .. / src 
OUT_PATH = .. / bin
VPATH = $ {SRC_PATH}
FILES = Foo.cpp Bar.cpp

CC = g ++
CFLAGS = -Wall -shared

全部:
mkdir -p $ {OUT_PATH}
$ {CC} $ {CFLAGS} -I $ {SRC_PATH} $ {FILES} -o $ {OUT_PATH} /MyPlugin.so

通过这样做,我试图避免定义 FILES 变量,如下所示:

  FILES = .. / src / Foo.cpp ../src/Bar.cpp 

当我试着运行 make all 时,g ++给了我一个错误。它看起来像通过 -I 标志指定的路径仅用于搜索 #include d个文件。

  g ++:Foo.cpp:没有这样的文件或目录
g ++:Bar.cpp:没有这样的文件或目录

我不能使用通配符( *。cpp ),因为我并不总是希望所有的文件都能被编译。另一种方法是将 cd 添加到src目录,如 here ,然后从那里运行g ++,但如果所有文件都在同一个目录下(我需要输出为单个),那么这只适用于我。所以文件)。我也尝试设置环境变量 PATH ,但这似乎没有任何作用。

我查看了g ++的帮助,make文档,看了一下StackOverflow的帖子,比如https://stackoverflow.com/questions/10010741/g-compile-with-codes-of-base-class- in-a-separate-directory gcc / g ++:"没有这样的文件或目录,但找不到我可以使用的任何解决方案。你能否建议我一个适合这个问题的方法?

编辑这个例子可能会产生误导。在这个精简的例子中,我将所有的源文件放在一个目录中,但在我的实际项目中,我有几个子目录,每个目录中有多个文件。因此,虽然在上面的例子中 cd src 目录工作,但它在我的实际项目中不起作用(或至少,我想知道它是如何工作的)。

解决方案

您可以为实际源文件,如下所示:

  FILES = Foo.cpp Bar.cpp 
SOURCES = $(文件:%。cpp = $(SRC_PATH)/%。cpp)

文件在构建您使用 SOURCES 时。


I am trying to setup a build process using makefiles, for a C++ plugin that I am working on. I am curious to know if there is any way to make g++ compile source files found in another directory. My motivation for doing this is to avoid having to specify the relative path for each source file, as I explain below.

My project directory structure looks like:

MyPlugin --> src   --> Foo.cpp
                   --> Foo.h
                   --> Bar.cpp
                   --> Bar.cpp
         --> build --> Makefile

Following is a stripped down version of my current Makefile:

SRC_PATH=../src
OUT_PATH=../bin
VPATH=${SRC_PATH}
FILES=Foo.cpp Bar.cpp

CC=g++
CFLAGS=-Wall -shared

all:
    mkdir -p ${OUT_PATH}
    ${CC} ${CFLAGS} -I${SRC_PATH} ${FILES} -o ${OUT_PATH}/MyPlugin.so

By doing this, I am trying to avoid defining the FILES variable as below:

FILES=../src/Foo.cpp ../src/Bar.cpp

When I tried running make all, g++ gave me an error. It looks like the path specified through -I flag is only used to search for #included files.

g++: Foo.cpp: No such file or directory
g++: Bar.cpp: No such file or directory

I cannot use wildcards (*.cpp) because I do not always want all the files to be picked up for compilation. Another alternative is to cd to the src directory, as mentioned here, and run g++ from there, but that only works for me if all the files are in the same directory (I need the output to be a single .so file). I also tried setting the environment variable PATH, but that didn't seem to have any effect.

I have looked into the g++ help, the make documentation, and looked at StackOverflow posts such as https://stackoverflow.com/questions/10010741/g-compile-with-codes-of-base-class-in-a-separate-directory and gcc/g++: "No such file or directory" , but could not find any solution which I could use. Could you please suggest me an suitable approach to this problem?

Edit The example may have been misleading. In this stripped down example, I have all the source files in one directory, but in my actual project, I have several subdirectories, and multiple files in each directory. Thus, although cding to the src directory works in my above example, it won't work in my actual project (or at least, I would like to know how it would work).

解决方案

You can have second variable for the actual source files, like this:

FILES = Foo.cpp Bar.cpp
SOURCES = $(FILES:%.cpp=$(SRC_PATH)/%.cpp)

Then instead of using FILES when building you use SOURCES.

这篇关于g ++编译另一个目录中存在的源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆