使用CMake生成VS工程后,在编译的时候报如下错误:
在确认链接库的设置正确后,发现CMake生成的VS工程是64位的,而链接的库是32位的:
因此,在CMake生成VS工程的时候,需要加上编译参数,使生成32位的工程,编译参数如下:
cmake .. -G "Visual Studio 16 2019" -A Win32
-G 指示CMake指定的kit,根据电脑上已安装的编译器填写;
-A 指示CMake编译的版本,如32位为Win32,64位为x64。
新生成的VS工程:
若配合VS code使用CMake,则tasks.json可配置为:
"tasks":[{"label":"CMake","type":"shell","command":"cmake","args":["..","-G","Visual Studio 16 2019","-A","Win32"],"options":{"cwd":"${workspaceFolder}/build"},"group":"build","dependsOn":[],"problemMatcher":"$msCompile"},{"label":"compile","type":"shell","command":"cmake --build .","options":{"cwd":"${workspaceFolder}/build"},"group":"build","presentation":{"reveal":"always","clear":true},"problemMatcher":"$msCompile"}]
参考资料: