我正在ANSI-C
Microsoft Windows [Version 10.0.14393]
上使用msbuild
版本Microsoft (R)-Buildmodul, Version 15.1.1012.6693
我可以使用下面显示的设置编译所有内容。
项目文件夹的结构如下:
- projectfolder
- a.vcxproj
- main.c
- main.h
- other.c
- other.h
- test.c
- test.h
的main.c
#include <stdio.h>
#include "main.h"
#include "test.h"
int main() {
printf("Hello World\n");
test_printf();
return 0;
}
main.h
#ifndef MAIN_H_
#define MAIN_H_
#endif // !MAIN_H_
other.c
#include "other.h"
#ifdef dummy
int some_number = 3;
#endif // dummy
other.h
#ifndef OTHER_H_
#define OTHER_H_
#define dummy
#ifdef dummy
extern int some_number;
#endif // dummy
#endif // OTHER_H_
test.c的
#include <stdio.h>
#include "test.h"
extern int test_printf(void) {
printf("Hello from test.c");
return 0;
}
test.h
#ifndef TEST_H_
#define TEST_H_
extern int test_printf(void);
#endif // !TEST_H_
构建脚本a.vcxproj
看起来像这样(由Visual Studio 2017 Community
生成,然后在编辑器中根据我的需要调整):
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{25F5E5F8-5F52-4BFC-A822-766DCFC1F5BA}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>a</RootNamespace>
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>bin\$(Configuration)\</OutDir>
<IntDir>bin\$(Configuration)\obj\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
<BuildLog>
<Path>bin\$(Configuration)\$(MSBuildProjectName).log</Path>
</BuildLog>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="other.c">
<DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DisableLanguageExtensions>
<CompileAs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">CompileAsC</CompileAs>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClCompile Include="test.c">
<DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DisableLanguageExtensions>
<CompileAs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">CompileAsC</CompileAs>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.c">
<DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DisableLanguageExtensions>
<CompileAs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">CompileAsC</CompileAs>
</ClCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>
**这个编译没有警告/ rrors到* .exe **
但是我想为每个生成的目标文件(main.obj,other.obj和test.obj)指定编译器允许在编译步骤中使用的源。
如果我将每个<ItemGroup>
更改为此(每个生成* .obj文件,获取它应该用于编译步骤的* .c文件:
<ItemGroup>
<ClCompile Include="other.c"> <!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DisableLanguageExtensions>
<CompileAs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">CompileAsC</CompileAs>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClCompile Include="test.c;other.c"> <!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DisableLanguageExtensions>
<CompileAs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">CompileAsC</CompileAs>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.c;other.c;test.c"> <!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<DisableLanguageExtensions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DisableLanguageExtensions>
<CompileAs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">CompileAsC</CompileAs>
</ClCompile>
</ItemGroup>
然后我得到以下编译器和链接器警告:
InitializeBuildStatus:
"bin\Debug\obj\a.tlog\unsuccessfulbuild" wird erstellt, da "AlwaysCreate" angegeben wurde.
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppBuild.targets(937,5): warning MSB8027: Two or more files
with the name of other.c will produce outputs to the same location. This can lead to an incorrect build result. The files involved are other.c, other.c, o
ther.c. [C:\Users\user\Desktop\question\a\a.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppBuild.targets(937,5): warning MSB8027: Two or more files
with the name of test.c will produce outputs to the same location. This can lead to an incorrect build result. The files involved are test.c, test.c.
链接:
bin\Debug\obj\other.obj : warning LNK4042: object specified more than once; extras ignored [C:\Users\user\Desktop\question\a\a.vcxproj]
bin\Debug\obj\other.obj : warning LNK4042: object specified more than once; extras ignored [C:\Users\user\Desktop\question\a\a.vcxproj]
bin\Debug\obj\test.obj : warning LNK4042: object specified more than once; extras ignored [C:\Users\user\Desktop\question\a\a.vcxproj]
构建成功:
"C:\Users\user\Desktop\question\a\a.vcxproj" (Standardziel) (1) ->
(WarnCompileDuplicatedFilename Ziel) ->
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppBuild.targets(937,5): warning MSB8027: Two or more fil
es with the name of other.c will produce outputs to the same location. This can lead to an incorrect build result. The files involved are other.c, other.c,
other.c. [C:\Users\user\Desktop\question\a\a.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppBuild.targets(937,5): warning MSB8027: Two or more fil
es with the name of test.c will produce outputs to the same location. This can lead to an incorrect build result. The files involved are test.c, test.c. [C
:\Users\user\Desktop\question\a\a.vcxproj]
"C:\Users\user\Desktop\question\a\a.vcxproj" (Standardziel) (1) ->
(Link Ziel) ->
bin\Debug\obj\other.obj : warning LNK4042: object specified more than once; extras ignored [C:\Users\user\Desktop\question\a\a.vcxproj]
bin\Debug\obj\other.obj : warning LNK4042: object specified more than once; extras ignored [C:\Users\user\Desktop\question\a\a.vcxproj]
bin\Debug\obj\test.obj : warning LNK4042: object specified more than once; extras ignored [C:\Users\user\Desktop\question\a\a.vcxproj]
5 Warnung(en)
0 Fehler
在msbuild
中是否有办法实现我喜欢做的事情:为每个源文件告诉msbuild它允许用于编译步骤的内容?