2

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?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
user1580780
  • 29
  • 1
  • 1
  • 1
    Make two libraries. One that's for C++98, and one that's for C++11. That way people who are using C++98 can link against the C++98 library, and those people using C++11 can link against the C++11 library. – Eljay Jan 06 '22 at 16:46
  • `set_source_file_properties` – KamilCuk Jan 06 '22 at 16:47
  • The Standard Library headers changed between C++98 and C++11 so code will not be compatible between the 2 versions. – Richard Critten Jan 06 '22 at 17:22

1 Answers1

1

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.

blurryroots
  • 402
  • 5
  • 12
  • I'm increasingly thinking I asked the wrong question. Let's say I compiled the Cpp11 library, and then wanted to reference it from the cpp98 library. I add the cpp11 library to the cpp98 library in cmake, and then in one of my header files in cpp98, I try to include the .h file from cpp11 library. When I go to compile, I get all sorts of errors related to C++ 11. How would I use the cpp11 library in the cpp98 library? – user1580780 Jan 10 '22 at 17:43
  • Alternatively, a different question that would work for me: what I actually have is an externally compiled C++ 11 library that I got from elsewhere. It has extern C functions set up, I just need to be able to add it and compile it. However, when I add the library in cmake, and then include the .hpp file from it, I get the same C++ 11 errors when I compile as in the previous comment. – user1580780 Jan 10 '22 at 17:46
  • @user1580780 I've adjusted the example, to show how to combine the different libraries into a common library or an executable. Theoretically, you can combine whatever library you want. It gets tricking however, if you have [different compiler versions](https://stackoverflow.com/questions/1420284/link-libraries-compiled-by-various-compilers) involved. You also have to look out for not pulling in features of the c++11 library, which are not supported in c++98. Otherwise you might have to [do some backporting](https://stackoverflow.com/questions/26266771/converting-c11-into-equivalent-c98-code). – blurryroots Jan 11 '22 at 15:33
  • I appreciate the response, but I'm having a hard time seeing how that answers either of my questions. How can I call the C++ 11 library from the C++ 98 library? Like I said, I get errors when I include the .hpp file (or a .h file that references the .hpp file). – user1580780 Jan 11 '22 at 21:04
  • @user1580780 I can see that. The issue with complex problems, is that often times we don't know how much more there is to know. There is no simple answer to ```How can I call the C++ 11 library from the C++ 98 library?``` and asking such shows a lack of initiative, especially after all that has been said. To put it bluntly, please take your time to actually look at the resources provided here, instead of asking for other people to do the work for you. At this point, I would recommend closing this question, doing a bit more work and create a follow-up question with proper details and clearity. – blurryroots Jan 12 '22 at 12:56