从命令行编译时添加额外的库和包含路径

18

我正在尝试在编译时为我的项目组添加其他路径。由于使用,因此我已经查看了相关文档,并根据我所找到的,应该可以作为属性传递。例如:

msbuild /p:AdditionalLibPaths=C:\FooBar\Libs /t:build foo.groupproj

但似乎没有使用我添加的路径。我之前注意到,当传递给msbuild时,VC++C++ Builder之间的某些属性名称不同,所以我想知道C++ Builder是否会使用其他属性名称来添加附加的lib和include文件夹?
我不想替换项目中定义的现有路径,而是要追加额外的路径。这样做的原因是,在我们的构建服务器上构建项目时,一些库位于标准位置,可能与开发机器上安装的位置不同。
实际上,msbuild调用一个msbuild脚本文件,该文件再调用其他脚本,包括使用标签的.groupproj脚本。我知道在使用标签时会创建一个新的msbuild实例,所以我知道必须在运行脚本中的该任务时添加属性。
<MSBuild Targets="Build" Projects="..\Foo.groupproj" Properties="Config=Debug (property to add additional paths here!)" />

更新:

C++ Builder 似乎正在使用 IncludePathILINK_LibraryPath,但设置这些会覆盖项目文件中已定义的路径。由于该文件是由 IDE 创建和维护的,因此任何更改使其附加而不是覆盖都将被 IDE 覆盖。这有点奇怪,因为它看起来确实应该附加这些值。

<IncludePath>..\FooBar\;$(BDS)\include;$(BDS)\include\dinkumware;$(BDS)\include\vcl;Common Components;..\Config\Config32;$(IncludePath)</IncludePath>

更新2:

CodeGear.Cpp.Targets 中,我添加了自己的 属性,名为 AdditionalIncludePaths,并对包含路径进行了修改。

大约在第251行附近

<PropertyGroup>
        <BCC_NoLink>true</BCC_NoLink>
        <ILINK_OSVersion Condition="'$(ILINK_OSVersion)'=='' And '$(NoVCL)'!='true'">5.0</ILINK_OSVersion>
        <DCC_GenerateCppFiles>true</DCC_GenerateCppFiles>
        <ShowStdOut Condition="'$(ShowStdOut)'==''">$(ShowGeneralMessages)</ShowStdOut>

        <!-- _TCHAR mapping for Uni^H^H^H character selection -->
        <StartupObj Condition="'$(_TCHARMapping)'=='wchar_t'">$(StartupObj)w</StartupObj>
        <ILINK_StartupObjs Condition="'$(ILINK_StartupObjs)'==''">$(StartupObj)</ILINK_StartupObjs>
        <BCC_GenerateUnicode Condition="'$(_TCHARMapping)'=='wchar_t'">true</BCC_GenerateUnicode>
        <!-- Include Paths -->
        <Win32LibraryPath Condition="'$(Win32LibraryPath)'==''">$(BDS)\lib</Win32LibraryPath>
        <IncludePath Condition="'$(CBuilderIncludePath)'!=''">$(IncludePath);$(CBuilderIncludePath)</IncludePath>
                <IncludePath Condition="'$(AdditionalIncludePath)'!=''">$(IncludePath);$(AdditionalIncludePath)</IncludePath>
        <BCC_IncludePath Condition="'$(BCC_IncludePath)'!=''">$(BCC_IncludePath);$(IncludePath)</BCC_IncludePath>
        <BCC_IncludePath Condition="'$(BCC_IncludePath)'==''">$(IncludePath)</BCC_IncludePath>
        <BRCC_IncludePath Condition="'$(BRCC_IncludePath)'!=''">$(BRCC_IncludePath);$(IncludePath)</BRCC_IncludePath>
        <BRCC_IncludePath Condition="'$(BRCC_IncludePath)'==''">$(IncludePath)</BRCC_IncludePath>
        <DCC_IncludePath Condition="'$(DCC_IncludePath)'!=''">$(DCC_IncludePath);$(IncludePath)</DCC_IncludePath>
        <DCC_IncludePath Condition="'$(DCC_IncludePath)'==''">$(IncludePath)</DCC_IncludePath>
        <DCC_UnitSearchPath>$(DCC_IncludePath);$(Win32LibraryPath)</DCC_UnitSearchPath>
        <DCC_ResourcePath>$(DCC_IncludePath)</DCC_ResourcePath>
        <DCC_ObjPath>$(DCC_IncludePath)</DCC_ObjPath>
        <TASM_IncludePath Condition="'$(TASM_IncludePath)'!=''">$(TASM_IncludePath);$(IncludePath)</TASM_IncludePath>
        <TASM_IncludePath Condition="'$(TASM_IncludePath)'==''">$(IncludePath)</TASM_IncludePath>

然后我可以调用。
msbuild /t:build /p:AdditionalIncludePaths=C:\Foo\Include foo.groupproj

这个很好用,实现了我想要的功能。我只需要用相同的方法处理库路径就可以了。但是我不想像这样篡改Embarcadero提供的文件之一。这太荒谬了:P…难道没有官方属性可以设置来添加包含路径和库路径吗?

4个回答

11

对于VS2013,只需在运行msbuild之前定义环境变量:

set "INCLUDE=%additional_include_path%;%INCLUDE%"
set "LIB=%additional_lib_path%;%LIB%"
REM use environment variables for INCLUDE and LIB values
set UseEnv=true

参考:MSBuild/Microsoft.Cpp/v4.0/V120/Microsoft.Cpp.targets

<Target Name="SetBuildDefaultEnvironmentVariables"
        Condition="'$(UseEnv)' != 'true'">
...
    <SetEnv Name   ="INCLUDE"
        Value  ="$(IncludePath)"
        Prefix ="false" >
       <Output TaskParameter="OutputEnvironmentVariable"             PropertyName="INCLUDE"/>
    </SetEnv>

但是看起来INCLUDE和LIB被附加到项目属性中指定的其他包含/库目录后面。


6

如果需要在VS2019中添加额外的include路径,请使用开关/p:IncludePath=C:\Foo

如果需要包含多个路径,请在开关中使用双引号和分号:

/p:IncludePath="C:\Foo;C:\Bar;C:\Another;$(IncludePath)"

1
多条路径怎么样? - X.Arthur
@X.Arthur,你可以这样做(注意双引号和分号):/p:IncludePath="C:\Foo;C:\Bar;C:\Another;$(IncludePath)" - flydev

3
在C++Builder 10 Seattle(截至2016年的当前版本)中,我能够通过在运行msbuild之前将附加库路径放入环境变量ILink_LibraryPath中来解决这个问题(即添加自定义库路径到自动化构建中)。这必须通过set ILink_LibraryPath=...而不是将属性作为/p:...传递给msbuild来完成。
这将在自动化构建环境中实现附加路径,而不会替换已设置在.cbproj文件中的现有路径,并且不需要在Embarcadero提供的文件中进行任何黑客操作。
这种方法唯一的问题是无法保证检查单个路径的顺序 - 即通过环境变量提供的自定义路径会被附加到.cbproj路径或可能放在中间,具体取决于项目设置,并不一定放在前面,因此您需要注意不要在项目文件中提及的其他目录中具有冲突的库。

非常有用的回答,谢谢!关于这个环境变量,如果我们需要提供多个值路径,语法会是什么样子呢? - user6167676
@nk-fford 用分号将多个路径分开。 - Cozzamara

1
对于VS2019,我测试了msbuild,发现即使你已经设置了INCLUDELIB的环境变量,它也不会应用。其中的原因可能是当加载和编译*.sln或者*.vcxproj文件时,msbuild覆盖了INCLUDELIB。但这仅是我的猜测,因为关于Windows/msbuild的在线文档非常少,很难找到根本原因。
解决方案A:
在我的解决方案中,我使用CLLINK的环境变量来在msbuild构建项目之前设置包含目录和库目录:
set $env:CL="/I\C:\users\user\local\include"
set $env:LINK="/LIBPATH:C:\users\user\local\lib"
...
msbuild *.vcxproj

关于CL的更多信息,请参见MSVC编译器环境变量
关于LINK的更多信息,请参见MSVC链接器环境变量

解决方案B:
对于您已经指出的另一个可能的解决方案,您可以像这样将选项传递给msbuild

msbuild *.vcxproj /nologo /p:AdditionalIncludePaths="C:\users\user\local"
msbuild *.vcxproj /nologo /p:IncludePath="C:\users\user\local"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接