Different compile flags for same file in different targets

前端 未结 5 526
臣服心动
臣服心动 2020-12-18 19:08

I would like to include a .cpp-file in two different targets (becoming two VS projects after running CMake). I would like to set different COMPILE_FLAGS for these projects.<

5条回答
  •  我在风中等你
    2020-12-18 19:55

    If you adhere to a one target per subdirectory philosophy, you could do the following using add_definitions to add your compile flags.


    # in ./CMakeLists.txt
    add_subdirectory(project1)
    add_subdirectory(project2)   
    

    # in ./project1/CMakeLists.txt:
    add_definitions("flags1")
    add_executable(project1 ../myfile.cpp)
    

    # in ./project2/CMakeLists.txt:
    add_definitions("flags2")
    add_executable(project2 ../myfile.cpp)
    

    add_definitions applies to all files compiled in this subdirectory and those under it. You can apply flags to specific files using the following:

    set_source_files_properties(myfile.cpp PROPERTIES COMPILE_FLAGS "flags")
    

提交回复
热议问题