背景

在 VS 中创建 Visual C++ --> Cross Platform --> Android | Linux --> Makefile Project 后,只是在项目中有这样一段代码:

LOGD("data length error: %d", datalen); <-- 字符串: “error:”

编译的时候就会报错:

问题重现

涉及的主要代码

下面这一行就是出错的地方:

LOGD("data length error: %d", datalen); <-- 字符串: “error:”

这段代码位于 ​​timetest.c​​ :

#include <stdio.h>
#include <stdlib.h>

#define LOG_ID_RADIO 1
#define ANDROID_LOG_DEBUG 3
#define LOG_TAG "TEST_LOG"

#define LOGD(...) printf(ANDROID_LOG_DEBUG,LOG_TAG ,__VA_ARGS__);

int main()
{
size_t datalen = 0;
LOGD("data length errdor: %d", datalen);
return 0;
}

由 ​​build.bat​​ 脚本编译执行:

cd D:\90tmp\dddr\TestCC\jni
echo ====
call ndk-build -C D:/90tmp/dddr/TestCC/jni -B
echo error=%ERRORLEVEL%

Error Log

1>------ Build started: Project: Project14, Configuration: Debug Win32 ------
1>====
1>make: Entering directory `C:/workspace/ndkTest/ndk-build/TestCC/jni'
1>[arm64-v8a] Compile : testtime <= timetest.c
1>C:/workspace/ndkTest/ndk-build/TestCC/jni/./timetest.c(16,2): warning G5552ABC2: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
1>EXEC : LOGD("data length error : %d", datalen);
1> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1>C:/workspace/ndkTest/ndk-build/TestCC/jni/./timetest.c:10:26: note: expanded from macro 'LOGD'
1>#define LOGD(...) printf(ANDROID_LOG_DEBUG,LOG_TAG ,__VA_ARGS__);
1> ^~~~~~~~~~~~~~~~~
1>C:/workspace/ndkTest/ndk-build/TestCC/jni/./timetest.c:7:27: note: expanded from macro 'ANDROID_LOG_DEBUG'
1>#define ANDROID_LOG_DEBUG 3
1> ^
1>C:/Users/v-shenya/AppData/Local/Android/Sdk/ndk-bundle/build//../sysroot/usr/include\stdio.h:129:24: note: passing argument to parameter '__fmt' here
1>int printf(const char* __fmt, ...) __printflike(1, 2);
1> ^
1>1 warning generated.
1>[arm64-v8a] Executable : testtime
1>[arm64-v8a] Install : testtime => libs/arm64-v8a/testtime
1>make: Leaving directory `C:/workspace/ndkTest/ndk-build/TestCC/jni'
1>error=0
1>Press any key to continue . . .
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Microsoft.MakeFile.Targets(44,5): error MSB3073: The command "C:\workspace\ndkTest\ndk-build\TestCC\jni\build.bat" exited with code -1.
1>Done building project "Project14.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

但是只要不把 “string" 和 “:" 放在一起,编译就没有问题:

1>------ Build started: Project: Project2, Configuration: Debug x86 ------

1>====

1>make: Entering directory `C:/Users/mdTestAU/Desktop/23922-testcc/TestCC/jni'

1>C:/Users/mdTestAU/Desktop/23922-testcc/TestCC/jni/./timetest.c(24,1): warning G5552ABC2: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]

1>LOGD("data length errdor: %d", datalen);

1>^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1>C:/Users/mdTestAU/Desktop/23922-testcc/TestCC/jni/./timetest.c:14:26: note: expanded from macro 'LOGD'

1>#define LOGD(...) printf(ANDROID_LOG_DEBUG,LOG_TAG ,__VA_ARGS__);

1> ^~~~~~~~~~~~~~~~~

1>C:/Users/mdTestAU/Desktop/23922-testcc/TestCC/jni/./timetest.c:8:27: note: expanded from macro 'ANDROID_LOG_DEBUG'

1>#define ANDROID_LOG_DEBUG 3

1> ^

1>C:/ProgramData/Microsoft/AndroidNDK64/android-ndk-r15c/build//../sysroot/usr/include\stdio.h:144:45: note: passing argument to parameter here

1>int printf(const char * __restrict _Nonnull, ...) __printflike(1, 2);

1> ^

1>1 warning generated.

1>[arm64-v8a] Compile : testtime <= timetest.c

1>[arm64-v8a] Executable : testtime

1>[arm64-v8a] Install : testtime => libs/arm64-v8a/testtime

1>make: Leaving directory `C:/Users/mdTestAU/Desktop/23922-testcc/TestCC/jni'

1>error=0

1>Done building project "Project2.vcxproj".

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

原因

就目前来讲,在 VS 中在创建 Android Makefile project 和 Linux Makefile project 在 build 过程中,MSBuild 会把 ”string:" 自动检测为 error 并 放到 error list 中。

#解决方案 1:对所有类型的 Android|Linux Makefile project 生效

修改出错项目的 MSBuild 的 build 命令,让 MSBuild 编译器能够识别字符串 “string:"

找到出错的文件

//Android Makefile project 的文件是这个
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Application Type\Android\3.0\Android.Makefile.targets

copy 这个文件做个备份先

修改Android.Makefile.targets 里面的 Build、CoreClean、ReBuild 三个 Target

 <!-- Override the default build / rebuild / clean makefile targets to pass in our custom error / warning handling -->
<Target Name="CoreClean" DependsOnTargets="PrepareForNMakeBuild">
<VCMessage Code="MSB8005" Type="Warning" Arguments="NMakeCleanCommandLine" Condition="'$(NMakeCleanCommandLine)'==''"/>
<Exec Command="$(NMakeCleanCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeCleanCommandLine)'!=''"/>
</Target>

<Target Name="Build" DependsOnTargets="PrepareForNMakeBuild;ResolveReferences;GetTargetPath;$(PreNMakeBuildTarget)" Returns="$(NMakeManagedOutput)">
<VCMessage Code="MSB8005" Type="Warning" Arguments="NMakeBuildCommandLine" Condition="'$(NMakeBuildCommandLine)'==''"/>
<Exec Command="$(NMakeBuildCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeBuildCommandLine)'!=''"/>
</Target>

<Target Name="Rebuild" DependsOnTargets="PrepareForNMakeBuild;_SetRebuildReferences;ResolveReferences;GetTargetPath;$(PreNMakeBuildTarget)" Returns="$(NMakeManagedOutput)">
<VCMessage Code="MSB8005" Type="Warning" Arguments="NMakeReBuildCommandLine" Condition="'$(NMakeReBuildCommandLine)'=='' and ('$(NMakeCleanCommandLine)'=='' or '$(NMakeBuildCommandLine)'=='')"/>
<Exec Command="$(NMakeReBuildCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeReBuildCommandLine)'!=''"/>
<Exec Command="$(NMakeCleanCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeReBuildCommandLine)'=='' and '$(NMakeCleanCommandLine)'!='' and '$(NMakeBuildCommandLine)'!=''" />
<Exec Command="$(NMakeBuildCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeReBuildCommandLine)'=='' and '$(NMakeCleanCommandLine)'!='' and '$(NMakeBuildCommandLine)'!=''" />
</Target>

解决方案 2:对单独项目修改

方案一中修改的是 VS 的配置文件,就是说修改之后对所有该类型项目生效。但如果你只希望这个项目可以跑起来,不再做配置文件的改变的话也是可以的(VS 产品组推荐做法)

  1. 右击你的ndk-build 项目里,添加一个新的​​property sheet​​:
  2. MSBuild-MSB3073--error-in-VS-Android-Linux-Makefile-Project_#define

  3. 在这个​​property sheet​​ 你需要定义 ​​Build​​, ​​Rebuild​​ 和 ​​CoreClean​​ targets,这会自动覆盖默认的 ​​targets​​. 因为旧的 三个 ​​targets​​ 正则表达式不能正确检测字符串 ​​"error:"​​, 所以我们需要用新的正则表达式去替代它。最下面你会看到完整的代码,你只需要将里面的代码复制到你项目中的 ​​PropertySheet.props​​ 就可以了。
  4. 在你出错项目中的​​.vcxproj​​ 中添加如下代码:
 <Import Project="PropertySheet.props" />

MSBuild-MSB3073--error-in-VS-Android-Linux-Makefile-Project_#define_02

Reload the project and build again.

正则表达式代码:

Android Makefile prject --> PropertySheet.props

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Target Name="CoreClean" DependsOnTargets="PrepareForNMakeBuild">
<VCMessage Code="MSB8005" Type="Warning" Arguments="NMakeCleanCommandLine" Condition="'$(NMakeCleanCommandLine)'==''"/>
<Exec Command="$(NMakeCleanCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeCleanCommandLine)'!=''"/>
</Target>

<Target Name="Build" DependsOnTargets="PrepareForNMakeBuild;ResolveReferences;GetTargetPath;$(PreNMakeBuildTarget)" Returns="$(NMakeManagedOutput)">
<VCMessage Code="MSB8005" Type="Warning" Arguments="NMakeBuildCommandLine" Condition="'$(NMakeBuildCommandLine)'==''"/>
<Exec Command="$(NMakeBuildCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeBuildCommandLine)'!=''"/>
</Target>

<Target Name="Rebuild" DependsOnTargets="PrepareForNMakeBuild;_SetRebuildReferences;ResolveReferences;GetTargetPath;$(PreNMakeBuildTarget)" Returns="$(NMakeManagedOutput)">
<VCMessage Code="MSB8005" Type="Warning" Arguments="NMakeReBuildCommandLine" Condition="'$(NMakeReBuildCommandLine)'=='' and ('$(NMakeCleanCommandLine)'=='' or '$(NMakeBuildCommandLine)'=='')"/>
<Exec Command="$(NMakeReBuildCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeReBuildCommandLine)'!=''"/>
<Exec Command="$(NMakeCleanCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeReBuildCommandLine)'=='' and '$(NMakeCleanCommandLine)'!='' and '$(NMakeBuildCommandLine)'!=''" />
<Exec Command="$(NMakeBuildCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(NMakeReBuildCommandLine)'=='' and '$(NMakeCleanCommandLine)'!='' and '$(NMakeBuildCommandLine)'!=''" />
</Target>

</Project>

Linux Makefile prject --> PropertySheet.props

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup />
<ItemGroup />

<!--
Override the default build / rebuild / clean makefile targets to pass in our custom error / warning handling
-->
<Target Name="Build" DependsOnTargets="PrepareForNMakeBuild;ResolveReferences;GetTargetPath;PreBuildEvent;RemotePreBuildEvent;_ResolveRemoteTarget;">
<VCMessage Code="MSB8005" Type="Warning" Arguments="BuildCommandLine" Condition="'$(BuildCommandLine)'==''"/>

<LogMessage ResourceName="ExecuteLocalBuildNotConfigured" Type="Warning" Arguments="BuildCommandLine" Condition="'$(BuildCommandLine)'==''"/>

<Exec Command="$(BuildCommandLine)"
IgnoreStandardErrorWarningFormat="true"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
Condition="'$(BuildCommandLine)' != ''"/>

<LogMessage ResourceName="ExecuteBuildNotConfigured" Type="Warning" Arguments="RemoteBuildCommandLine" Condition="'$(RemoteBuildCommandLine)'==''"/>
<Execute Condition="'$(RemoteBuildCommandLine)' != ''"
Command="$(RemoteBuildCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
ProjectDir="$(ProjectDir)"
RemoteProjectDir="$(_ResolvedRemoteProjectDir)"
RemoteTarget="$(ResolvedRemoteTarget)"
RemoteFilesToCopyLocally="$(RemoteBuildOutputs)"
LocalDirectoryDefault="$(OutDir)"
IntermediateDir="$(IntDir)"
ParseOutput="$(ParseMakefilesOutput)"
Timeout="$(RemoteExecuteTimeout)">
</Execute>
</Target>



<Target Name="Rebuild" DependsOnTargets="PrepareForNMakeBuild;Clean;ResolveReferences;GetTargetPath;RemotePreBuildEvent;_ResolveRemoteTarget;">

<LogMessage ResourceName="ExecuteLocalRebuildNotConfigured" Type="Warning" Arguments="ReBuildCommandLine" Condition="'$(ReBuildCommandLine)'==''"/>

<Exec Command="$(ReBuildCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(ReBuildCommandLine)' != ''"/>

<LogMessage ResourceName="ExecuteRebuildNotConfigured" Type="Warning" Arguments="RemoteReBuildCommandLine" Condition="'$(RemoteReBuildCommandLine)'==''"/>
<Execute Condition="'$(RemoteReBuildCommandLine)' != ''"
Command="$(RemoteReBuildCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
ProjectDir="$(ProjectDir)"
RemoteTarget="$(ResolvedRemoteTarget)"
RemoteProjectDir="$(_ResolvedRemoteProjectDir)"
RemoteFilesToCopyLocally="$(RemoteBuildOutputs)"
LocalDirectoryDefault="$(OutDir)"
IntermediateDir="$(IntDir)"
ParseOutput="$(ParseMakefilesOutput)"
Timeout="$(RemoteExecuteTimeout)">
</Execute>
</Target>

<Target Name="CoreClean" DependsOnTargets="PrepareForNMakeBuild;_ResolveRemoteTarget">
<VCMessage Code="MSB8005" Type="Warning" Arguments="CleanCommandLine" Condition="'$(CleanCommandLine)'==''"/>

<LogMessage ResourceName="ExecuteLocalCleanNotConfigured" Type="Warning" Arguments="CleanCommandLine" Condition="'$(CleanCommandLine)'==''"/>
<Exec Command="$(CleanCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
Condition="'$(CleanCommandLine)' != ''"/>

<LogMessage ResourceName="ExecuteCleanNotConfigured" Type="Warning" Arguments="RemoteCleanCommandLine" Condition="'$(RemoteCleanCommandLine)'==''"/>
<Execute Condition="'$(RemoteCleanCommandLine)' != ''"
Command="$(RemoteCleanCommandLine)"
CustomErrorRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>fatal error|error):(?<TEXT>.*)"
CustomWarningRegularExpression="(?<FILENAME>[^:]*):(?<LINE>\d*):(?<COLUMN>\d*):\s*(?<CATEGORY>warning):(?<TEXT>.*)"
IgnoreStandardErrorWarningFormat="true"
ProjectDir="$(ProjectDir)"
RemoteProjectDir="$(_ResolvedRemoteProjectDir)"
RemoteTarget="$(ResolvedRemoteTarget)"
IntermediateDir="$(IntDir)"
ParseOutput="$(ParseMakefilesOutput)"
Timeout="$(RemoteExecuteTimeout)">
</Execute>
<!-- TODO: might need to clean stuff like the tlog files and caches -->
</Target>


</Project>