How to specify one file for a library to be compiled with C++ 11 instead of 98?
I'm working on a project where most of it has to be compiled in C++ 98, however, I have exactly one file that needs to be compiled with C++ 11 instead.
Current example code:
set(SOURCES
file1.cpp
file2.cpp
file3.cpp
)
add_library(examplelibraryname SHARED ${SOURCES})
I want to specify that file3.cpp
needs to be compiled with CXX STANDARD 11.
I tried doing a set_target_properties(examplelibraryname PROPERTIES CXX_STANDARD 11)
on the whole library, but unfortunately one of the other files can't compile with C++ 11.
I have tried specifying just the target file by using:
set_property(SOURCE file3.cpp PROPERTY CXX_STANDARD 11)
but unfortunately I still get the errors associated with not compiling it with C++ 11 (presumably because this line is not affecting the add_library
line).
Is there any way for me to specify that just that file for the library should use C++ 11?
I would go with what @Eljay suggested and create separate targets for the different standars.
project("Stackoverflow70610506")
set(USE_COMBINED 0)
# Build c++98 specific compilation units.
add_library(Cpp98 STATIC cpp98.cpp)
set_property(TARGET Cpp98 PROPERTY CXX_STANDARD 98)
# Build c++11 specific compilation units.
add_library(Cpp11 STATIC cpp11.cpp)
set_property(TARGET Cpp11 PROPERTY CXX_STANDARD 11)
# Create an c++98 executable.
add_executable(Stackoverflow70610506 main.cpp)
set_property(TARGET Stackoverflow70610506 PROPERTY CXX_STANDARD 98)
if(USE_COMBINED)
# Alternativly combine them into a common c++98 library.
add_library(Combined STATIC Cpp11 Cpp98)
set_property(TARGET Cpp11 PROPERTY CXX_STANDARD 98)
# And link the combined library.
target_link_libraries(Stackoverflow70610506 Combined)
else()
# Or link them both directly.
target_link_libraries(Stackoverflow70610506 Cpp98 Cpp11)
endif()
The downside is, that you might have to adjust the export / visibility behavior of the API in your compilation units / libraries.