Search code examples
c++cmakepoco

How to change C++14 standard requirement from POCO based project to C++17


I am building a project using Cmake and POCO, I am using vcpkg to manage my dependencies in ubuntu so at first this is how my cmake file looks like

cmake_minimum_required(VERSION 3.8)
project(web)
set(CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Poco COMPONENTS Net Util PDF SQL XML REQUIRED)
add_executable(web "main.cpp")
target_link_libraries(web PRIVATE Poco::Net Poco::PDF Poco::SQL 
Poco::XML)

but then I found out this is what is going on at the commandline

 /usr/bin/g++-7  -DPOCO_ENABLE_CPP14 -DPOCO_HAVE_FD_EPOLL - 
 DPOCO_STATIC -DPOCO_UNBUNDLED -D_DEBUG -D_FILE_OFFSET_BITS=64 - 
 D_LARGEFILE64_SOURCE -D_REENTRANT -D_THREAD_SAFE - 
 D_XOPEN_SOURCE=500 -isystem /home/pius/vcpkg/installed/x64- 
 linux/include -g   -std=gnu++17 -std=gnu++14 -MD -MT 
 CMakeFiles/web.dir/main.cpp.o -MF CMakeFiles/web.dir/main.cpp.o.d - 
 o

as you can POCO is adding a new definition -DPOCO_ENABLE_CPP14 and it is passing -std=gnu++14 to gcc by default since my code depends on C++17 string_view I want C++17, I have tried to edit my CMake file to look like this

cmake_minimum_required(VERSION 3.8)
project(web)
find_package(Poco COMPONENTS Net Util PDF SQL XML REQUIRED)
add_executable(web "main.cpp")
set(CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions(-std=gnu++17)
remove_definitions(-DPOCO_ENABLE_CPP14 -std=gnu++14)
target_link_libraries(web PRIVATE Poco::Net Poco::PDF Poco::SQL 
Poco::XML)

but still Cmake is still generating the same effect as before, the remove_definitions command is not working, is there anyway I can over write the default requirement imposed by POCO and use my own instead? thanks


Solution

  • Since you require CMake 3.8 You should use instead:

    target_compile_features(web PUBLIC cxx_std_17)
    

    ref:
    https://cmake.org/cmake/help/v3.8/manual/cmake-compile-features.7.html#requiring-language-standards
    https://cmake.org/cmake/help/v3.8/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html