在 Eclipse 中设置构建输出目录 - C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4882231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Build output directory in Eclipse - c++
提问by Shervanator
I have a program which consists of multiple projects in eclipse (working under ubuntu and the projects being in c++), these projects consist of a main executable file, and other shared objects files and static libs.
我有一个由 eclipse 中的多个项目组成的程序(在 ubuntu 下工作,项目在 c++ 中),这些项目由一个主要的可执行文件、其他共享对象文件和静态库组成。
I want all these projects when built to output their files to one common binary folder, instead of their respective debug folders. This is to make linking easier with the main executable. If there are better solutions please also feel free to share.
我希望所有这些项目在构建时将它们的文件输出到一个公共二进制文件夹,而不是它们各自的调试文件夹。这是为了使与主可执行文件的链接更容易。如果有更好的解决方案也欢迎分享。
采纳答案by athwaites
Unfortunately, I have found that the C/C++ Buildtab does not allow you to set the build location unless you are creating your own makefile.
不幸的是,我发现C/C++ Build选项卡不允许您设置构建位置,除非您正在创建自己的 makefile。
You've likely found that the Builder Settingstab under Project Properties>C/C++ Buildis all grayed out in a default C/C++ project. This is because CDT nominates the internal builder by default for new projects. To change this, you can go to Project Properties>C/C++ Build>Tool Chain Editorand change the Current Builderto Gnu Make Builder. Next, go to Project Properties>C/C++ Buildand change the Builder Typeto External Builder. You can now opt to create your own makefile for the project, if you like; though I'd recommend leaving CDT to build the makefile automatically for now.
您可能已经发现,在默认 C/C++ 项目中,Project Properties>C/C++ Build下的Builder Settings选项卡全部变灰。这是因为 CDT 默认为新项目指定内部构建器。要更改此设置,您可以转到Project Properties>C/C++ Build>Tool Chain Editor并将Current Builder更改为Gnu Make Builder。接下来,转到Project Properties>C/C++ Build并将Builder Type更改为External Builder。如果愿意,您现在可以选择为项目创建自己的 makefile;虽然我建议暂时让 CDT 自动构建 makefile。
I have the same project requirements of outputting to a /project_path/bin (though I still maintain separation between Debug and Release builds). To do this, I perform a copy operation on the output as a post-build step.
我对输出到 /project_path/bin 有相同的项目要求(尽管我仍然保持 Debug 和 Release 版本之间的分离)。为此,我对输出执行复制操作作为构建后步骤。
To do this, go to Project Properties>C/C++ Build>Settingsand select the Build Stepstab. In the Post-build stepsunder Command:, enter:
为此,请转到Project Properties>C/C++ Build>Settings并选择Build Steps选项卡。在Command:下的Post-build 步骤中,输入:
cp ${BuildArtifactFilePrefix}${BuildArtifactFileName} "/path/to/bin/directory/";
Obviously replacing the "/path/to/bin/directory/" as required.
显然根据需要替换“/path/to/bin/directory/”。
I personally prefer to maintain my project files in a workspace/builddirectory; copying binaries to a workspace/bindirectory and libraries to a workspace/libdirectory. At first I found this copy workaround to be an inconvenience, but have come to appreciate it because it isolates the interstitial build files from the final binary/library.
我个人更喜欢在工作空间/构建目录中维护我的项目文件;将二进制文件复制到工作空间/bin目录,将库复制到工作空间/lib目录。起初我发现这种复制解决方法很不方便,但我开始欣赏它,因为它将间隙构建文件与最终的二进制文件/库隔离开来。
For binaries, I would use:
对于二进制文件,我会使用:
cp ${BuildArtifactFilePrefix}${BuildArtifactFileName} "${WorkspaceDirPath}/bin/";
For libraries, I would use:
对于图书馆,我会使用:
cp ${BuildArtifactFilePrefix}${BuildArtifactFileName} "${WorkspaceDirPath}/lib/";
I include the variable "${BuildArtifactFilePrefix}" because CDT includes "lib" as a default prefix for static libraries, which I actually prefer.
我包括变量“${BuildArtifactFilePrefix}”,因为 CDT 包括“lib”作为静态库的默认前缀,我实际上更喜欢它。
You just need to ensure that the target directory exists before building; Eclipse/CDT will not create the directory for you.
您只需要在构建前确保目标目录存在即可;Eclipse/CDT 不会为您创建目录。
Also, just remember that these copies will be left behind in the /binor /libdirectory on clean, but overwritten on any subsequent rebuild.
另外,请记住,这些副本将在清理时留在/bin或/lib目录中,但在任何后续重建时都会被覆盖。
回答by CashCow
Try Project->Properties
尝试 Project->Properties
Under C/C++ Build->Settings
you have a tab called Build Artifact
.
在C/C++ Build->Settings
您下面有一个名为Build Artifact
.
Under there you have Artifact name
. This defaults as ${ProjName}
.
在那里你有Artifact name
。这默认为${ProjName}
.
Modify this to specify a relative directory path to where you actually want the final file to end up. So could be ../../lib/${ProjName}
修改它以指定您实际希望最终文件结束的位置的相对目录路径。所以可能是../../lib/${ProjName}
The intermediate files (.o and .d) will still build to the sub-directory (Debug or Release) but I guess it's better off if they are there anyway and it is only the finally built library for which you want to change the build path.
中间文件(.o 和 .d)仍将构建到子目录(Debug 或 Release),但我想如果它们在那里会更好,并且它只是您想要更改构建的最终构建的库小路。
If you find it inconvenient typing the relative path like this, I use Environment to create environment variables with relative paths taking me back to a "root". One of this I have is ${LIBDIR}
and this is a relative path from where the project gets built. It is usually used for linking in other libraries, but can also be used as a target. Then you would set Artifact Name to ${LIBDIR}/${ProjName}
which works well if you use different directories for debug and release builds.
如果您发现像这样输入相对路径不方便,我使用 Environment 创建环境变量,其中相对路径将我带回“根”。我拥有的其中一个是${LIBDIR}
,这是项目构建的相对路径。它通常用于链接其他库,但也可以用作目标。然后${LIBDIR}/${ProjName}
,如果您使用不同的目录进行调试和发布构建,您可以将 Artifact Name 设置为效果很好。
回答by kyb
Go to
去
Project Properties ->C/C++ Build ->Settings ->(tab) GCC C++ Linker
项目属性->C/C++ 构建->设置->(选项卡)GCC C++ 链接器
The command line pattern is shown on the right side
命令行模式显示在右侧
${COMMAND} ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX} ${OUTPUT} ${INPUTS}
Put in front of ${OUTPUT}
放在前面 ${OUTPUT}
${COMMAND} ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX} ${ProjDirPath}/bin/${OUTPUT} ${INPUTS}
or
或者
${COMMAND} ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX} MyMainProject/path/bin/ ${INPUTS}
From https://www.eclipse.org/forums/index.php?t=msg&th=207500&goto=665566&#msg_665566
来自https://www.eclipse.org/forums/index.php?t=msg&th=207500&goto=665566&#msg_665566
回答by Alexej S.
In my project the build path defaults to the Build Configuration name, so I could use a ${ConfigName} macro to retrieve the build path in a post-build step:
在我的项目中,构建路径默认为构建配置名称,因此我可以使用 ${ConfigName} 宏在构建后步骤中检索构建路径:
${workspace_loc:/${ProjName}}/${ConfigName}/${BuildArtifactFileName}
Then you can copy target binaries to your common binary folder, or do something other in the build folder of that particular configuration.
然后您可以将目标二进制文件复制到您的公共二进制文件夹,或者在该特定配置的构建文件夹中执行其他操作。
回答by Rik
Just happened to be working on something that led me down a similar path - so I'll offer it up as an alternate solution/reminder to myself:
碰巧正在做一些让我走上类似道路的事情 - 所以我将提供它作为对自己的替代解决方案/提醒:
In Eclipse (at least in Luna) the generated makefiles are actually rather decent and convenient. I personally like to create several build configurations (Release and Debug variants with 32 and 64 bit architectures), and supplement them with debug and run (F5 and Execute, respectively) configurations.
在 Eclipse 中(至少在 Luna 中),生成的 makefile 实际上相当不错和方便。我个人喜欢创建几个构建配置(具有 32 位和 64 位架构的发布和调试变体),并用调试和运行(分别为 F5 和执行)配置来补充它们。
To continue: I have been toying with packaging on Debian and found - during the act of said toying - that I needed to create and test an install target. Eclipse neither generates for you, nor provides an interface to - a configuration - for customizing or adding an install target; Other than a place where you can specify that another target exists.
继续:我一直在玩 Debian 上的打包,并发现 - 在上述玩弄的过程中 - 我需要创建和测试安装目标。Eclipse 既不为您生成,也不提供用于自定义或添加安装目标的接口(配置);除了可以指定另一个目标存在的地方。
So technically Eclipse does provide an interface; kinda. Hence, I stumbled across the makefile.init, makefile.defs, and makefile.targetsfiles.
所以从技术上讲,Eclipse 确实提供了一个接口;有点。因此,我偶然发现了makefile.init、makefile.defs和makefile.targets文件。
Process/Workflow:
流程/工作流程:
Create a file
makefile.targets
in the root directory of your eclipse project; In said file define an install target manually. This - of course - allows you to specify every little detail as you'd like, but with the added benefit of all of the configuration provided by Eclipse already complete and available to you for use with defining the rules for the specified target.After defining the new target in the
makefile.targets
file, right clickon your project's name or main cpp file in Eclipse'sproject explorer
, and then selectMake Targets
->Build...
, and finallyAdd
to instantiate a pop-up. Alternatively, you could select 'create' in the last step instead of 'build' and it would provide the same pop-up required for the next part.Add the nameof your new target and - leaving everything else at their default values - clickok
If you chose to add the new make target by right-clickingin Project Explorerand selecting Make Target->Build..., after addingthe new make target you will be brought back to the first pop-up which resulted from selecting Build.... Otherwise, find your way to the
Make Targets
->Build..
pop-up now. Select the desired target and then click onBuild
.
makefile.targets
在你的eclipse项目的根目录下创建一个文件;在所述文件中手动定义安装目标。这当然允许您根据需要指定每一个小细节,但是 Eclipse 提供的所有配置已经完成并可供您用于定义指定目标的规则,这带来了额外的好处。在文件中定义好新目标后
makefile.targets
,在Eclipse的 中右键单击你的项目名称或主cpp文件project explorer
,然后选择Make Targets
->Build...
,最后Add
实例化一个弹出窗口。或者,您可以在最后一步中选择“创建”而不是“构建”,它会提供下一部分所需的相同弹出窗口。添加新目标的名称- 将其他所有内容保留为默认值 - 单击ok
如果您选择通过在Project Explorer 中右键单击并选择Make Target-> Build... 来添加新的 make 目标,则在添加新的 make 目标后,您将返回到选择Build产生的第一个弹出窗口。 ..。否则,请立即找到前往->弹出窗口的方法。选择所需的目标,然后单击。
Make Targets
Build..
Build
Looking through Eclipse's auto-generated makefiles was an excellent way to learn the makefile syntax and overall structure, and get into some advanced usage of includes and conditionals.
查看 Eclipse 自动生成的 makefile 是学习 makefile 语法和整体结构以及深入了解包含和条件的一些高级用法的极好方法。
Here are some portions of an example makefile, which - at least I hope - will demonstrate manually setting the output directory of a build:
以下是示例 makefile 的一些部分,至少我希望它会演示手动设置构建的输出目录:
prefix = /usr/local
bindir = $(prefix)/bin
sharedir = $(prefix)/share
mandir = $(sharedir)/man
man1dir = $(mandir)/man1
...
# Typical all target
all: <binaryname>
#Typical clean target
clean:
rm -f <binaryname> <objectname>.o
# Target invokes all, then installs to specified locations
install: all
install <binaryname> $(DESTDIR)$(bindir)
install -m 0644 <objectname>.1 $(DESTDIR)$(man1dir)
回答by Patrick Costello
If you open up the project's properties, there is a tab C/C++ Build. This has an option for build location, where you can specify the build directory. It seems you could change this for your multiple projects so that they share the same build directory.
如果您打开项目的属性,则会有一个选项卡 C/C++ Build。这有一个构建位置选项,您可以在其中指定构建目录。似乎您可以为多个项目更改此设置,以便它们共享相同的构建目录。