在windows上使用cmake3.23.2编译时,出现如下错:
CMake Error at cmake/ucm.cmake:579 (add_executable):
Target "gpuvis" links to item " C:/Program Files
(x86)/SDL2-2.0.5/lib/x64/SDL2.lib" which has leading or trailing
whitespace. This is now an error according to policy CMP0004.
Call Stack (most recent call first):
CMakeLists.txt:147 (ucm_add_target)```
查找cmake的官方文档,里面对CMP004的解释如下:
Libraries linked may not have leading or trailing whitespace.
CMake versions 2.4 and below silently removed leading and trailing whitespace from libraries linked with code like.
target_link_libraries(myexe " A ")
This could lead to subtle errors in user projects.
The OLD behavior for this policy is to silently remove leading and trailing whitespace. The NEW behavior for this policy is to diagnose the existence of such whitespace as an error. The setting for this policy used when checking the library names is that in effect when the target is created by an add_executable() or add_library() command.
This policy was introduced in CMake version 2.6.0. CMake version 3.23.2 warns when the policy is not set and uses OLD behavior. Use the cmake_policy() command to set it to OLD or NEW explicitly.
看起来是默认是OLD policy,应该只是warning而不是error,即使在Cmakelist里额外加入了cmake_policy -> OLD,还是不管用,跟官方文档里说的不一致,奇怪~~
cmake_minimum_required( VERSION 2.8 FATAL_ERROR )
cmake_policy(SET CMP0004 OLD)
Anyway,不管这个了,还是找找错在哪里吧~
查了一些资料,发现有人遇到同样的问题:
他在Cmakelist里去掉空格就解决了:
set(SDL2_LIBRARIES "-L${SDL2_LIBDIR} -lSDL2 ") <---- here
BEFORE: set(SDL2_LIBRARIES "-L${SDL2_LIBDIR} -lSDL2 ")
AFTER : set(SDL2_LIBRARIES "-L${SDL2_LIBDIR} -lSDL2")
但是我的Cmakelist并没有这样的空格…
由于我用的cmake的gui,我又检查了一下这个里面设置的path:
发现SDL2_LIBRARY有两个path,中间有空格,试试删掉:
竟然解决了~~~
耶!!!