CMake覆盖本机编译器标志

时间:2016-06-29 11:09:20

标签: cmake

我正在尝试使用Visual Studio 14使用CMake构建一个c项目但是我无法覆盖由CMake设置的导致项目无法构建的编译器标志。我在我的CMakeLists.txt文件中使用此方法

set(flags /nologo /c /EHsc /GS- /MTd /Od /TC /Zi /Zp2 /D _USING_V110_SDK71_)
set(CMAKE_C_FLAGS ${flags})
set(rc_flags /l0x809)
set(CMAKE_RC_FLAGS ${rc_flags})
set(linker_flags /NOLOGO /SUBSYSTEM:console /MACHINE:I386 /DLL)
set(CMAKE_LINKER_FLAGS ${linker_flags})

非常感谢任何帮助!

编辑:主要CMakeLists.txt的完整示例在这里

cmake_minimum_required(VERSION 3.4)
project(cuism C)

set(WINDOWS_SDK_INCLUDES "C:/Program Files\ (x86)/Microsoft\  SDKs/Windows/v7.1A/Include/")
set(WINDOWS_SDK_LIBRARIES "C:/Program Files\ (x86)/Microsoft\ SDKs/Windows/v7.1A/Lib")
set(WSOCK "WSock32.lib")
set(ODBC "odbc32.lib")
set(ODBCCP "odbccp32.lib")

set(includeDir "${CMAKE_SOURCE_DIR}/include/")
set(resourceDir "${CMAKE_SOURCE_DIR}/resources/")


include_directories(
    ${includeDir}
    ${resourceDir}
)

set(CMAKE_LIBRARY_PATH ${WINDOWS_SDK_LIBRARIES})
set(CMAKE_INCLUDE_PATH ${WINDOWS_SDK_INCLUDES})


set(flags "/nologo /c /EHsc /GS- /MTd /Od /TC /Zi /Zp2 /D _USING_V110_SDK71_" )
set(CMAKE_C_FLAGS ${flags})

set(rc_flags /l0x809)
set(CMAKE_RC_FLAGS ${rc_flags})

set(linker_flags /NOLOGO /SUBSYSTEM:console /DLL)
set(CMAKE_LINKER_FLAGS ${linker_flags})

message(c flags are ${CMAKE_C_FLAGS})
message(rc flags are ${CMAKE_RC_FLAGS})
message(linker flags are ${CMAKE_LINKER_FLAGS})
message(linker EXE flags are ${CMAKE_EXE_LINKER_FLAGS})

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)


ADD_SUBDIRECTORY(cuimbct)
ADD_SUBDIRECTORY(cuimb)
ADD_SUBDIRECTORY(cuidll)

每个sudbdirectory都有一个与此类似的CMakeLists.txt

set(src cuimb.c "${includeDir}port.c" cuimben.c cuimb.rc)
add_library(cuimb SHARED ${src})
target_link_libraries(cuimb ${WSOCK} ${ODBC} ${ODBCCP})

由于错误代码LNK2019

,在链接阶段出错

1 个答案:

答案 0 :(得分:1)

我尝试了您的代码,并且 - 经过一些小的修改 - 可以成功应用这些编译器/链接器选项。它包括I和@Tsyvarev建议的更改(引用和使用CMAKE_SHARED_LINKER_FLAGS),并且我已删除对Windows SDK 7.1的静态引用。可以使用-T工具集选项设置:

> cmake -H"." -B"buildWin7" -T"v140_xp" -G"Visual Studio 14 2015" 
...

<强>的CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(cuism C)

set(includeDir "${CMAKE_SOURCE_DIR}/include/")
set(resourceDir "${CMAKE_SOURCE_DIR}/resources/")

include_directories(
    ${includeDir}
    ${resourceDir}
)

set(flags "/nologo /c /EHsc /GS- /MTd /Od /TC /Zi /Zp2" )
set(CMAKE_C_FLAGS "${flags}")

set(rc_flags "/l0x809")
set(CMAKE_RC_FLAGS "${rc_flags}")

set(linker_flags "/NOLOGO /SUBSYSTEM:console /DLL")
set(CMAKE_SHARED_LINKER_FLAGS "${linker_flags}")

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

...

<强> cuimb \的CMakeLists.txt

...

add_library(cuimb SHARED ${src})
target_link_libraries(cuimb wsock32 odbc32 odbccp32)

建议

要保持CMake脚本代码清洁,您应该将所有编译器/链接器选项移动到工具链文件中。我已经添加了一些我通常在那里定义的选项。另外,我将其缩减为单个配置MyDebug(仅Debug会附加其他调试选项),因为您的选项似乎不适用于Release

> cmake -H"." -B "buildWin7" -DCMAKE_TOOLCHAIN_FILE:PATH="VS2015Toolchain.cmake" -G"Visual Studio 14 2015" 
...

<强> VS2015Toolchain.cmake

if (NOT CMAKE_GENERATOR_TOOLSET)
    set(CMAKE_GENERATOR_TOOLSET "v140_xp" CACHE INTERNAL "")
endif()

set(CMAKE_CONFIGURATION_TYPES "MyDebug" CACHE INTERNAL "")

# NOTE: Standard is a console app. If you need a windows app, 
#       use WIN32 define in add_executable
set(CMAKE_WIN32_EXECUTABLE 0 CACHE INTERNAL "")

set(CMAKE_C_FLAGS "/nologo /c /EHsc /GS- /MTd /Od /TC /Zi /Zp2 /W4" CACHE INTERNAL "")
set(CMAKE_RC_FLAGS "/l0x809" CACHE INTERNAL "")
set(
    CMAKE_SHARED_LINKER_FLAGS 
    "/NOLOGO /SAFESEH:NO /INCREMENTAL:NO /debug" 
    CACHE INTERNAL ""
)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH CACHE INTERNAL "")
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY CACHE INTERNAL "")
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY CACHE INTERNAL "")