Quantcast

CMAKE_CXX_STANDARD_REQUIRED not working in helloworld

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

CMAKE_CXX_STANDARD_REQUIRED not working in helloworld

winfried_mb2
I am trying to let cmake exit with an error when the c++ compiler
doesn't support at least the C++14 standard.
According to the cmake documentation this should be possible with
variables CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED.

However when I compile the simple helloworld example below with g++ 4.8
(which has no C++14 support), cmake supplies the option -std=c++1y to
g++ and compilation fails.
I expected CMake to stop during configure with an error.

Can anybody see what I am doing wrong ?

Thanks in advance,
Winfried


file main.cpp :

#include <string>
#include <iostream>

using namespace std::string_literals;

int main(int, char **)
{
     std::cout << "Hello "s + "world!" << std::endl;
     return 0;
}



file CMakeLists.txt :

project(reqstd_test)
cmake_minimum_required(VERSION 3.7)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(hello main.cpp)


cmake output:
wdobbe@linux-wdobbe:/data/generated/cmake_std_req/build> cmake ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /data/generated/cmake_std_req/build
wdobbe@linux-wdobbe:/data/generated/cmake_std_req/build>


make output:
wdobbe@linux-wdobbe:/data/generated/cmake_std_req/build> make VERBOSE=Y
/home/wdobbe/install/cmake-3.8.0-rc2-Linux-x86_64/bin/cmake
-H/data/generated/cmake_std_req -B/data/generated/cmake_std_req/build
--check-build-system CMakeFiles/Makefile.cmake 0
<cut>
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
/usr/bin/c++    -std=c++1y -o CMakeFiles/hello.dir/main.cpp.o -c
/data/generated/cmake_std_req/main.cpp
/data/generated/cmake_std_req/main.cpp:4:22: error: ‘string_literals’ is
not a namespace-name
  using namespace std::string_literals;
                       ^
/data/generated/cmake_std_req/main.cpp:4:37: error: expected
namespace-name before ‘;’ token
  using namespace std::string_literals;
                                      ^
/data/generated/cmake_std_req/main.cpp: In function ‘int main(int,
char**)’:
/data/generated/cmake_std_req/main.cpp:8:18: error: unable to find
string literal operator ‘operator"" s’
      std::cout << "Hello "s + "world!" << std::endl;
                   ^
CMakeFiles/hello.dir/build.make:62: recipe for target
'CMakeFiles/hello.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/hello.dir/main.cpp.o] Error 1
make[2]: Leaving directory '/data/generated/cmake_std_req/build'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/hello.dir/all'
failed
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
make[1]: Leaving directory '/data/generated/cmake_std_req/build'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: CMAKE_CXX_STANDARD_REQUIRED not working in helloworld

Robert Maynard
The issue is that GCC 4.8 c++1y does offer support for some c++14
features ( https://gcc.gnu.org/projects/cxx-status.html#cxx14 ) and
therefore that is why you getting a compile time failure. You could
try using target_compile_features instead but it only verifies
language level features and is not aware of standard library features
such as std::string_literals.

On Tue, Mar 7, 2017 at 9:32 AM, Winfried <[hidden email]> wrote:

> I am trying to let cmake exit with an error when the c++ compiler doesn't
> support at least the C++14 standard.
> According to the cmake documentation this should be possible with variables
> CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED.
>
> However when I compile the simple helloworld example below with g++ 4.8
> (which has no C++14 support), cmake supplies the option -std=c++1y to g++
> and compilation fails.
> I expected CMake to stop during configure with an error.
>
> Can anybody see what I am doing wrong ?
>
> Thanks in advance,
> Winfried
>
>
> file main.cpp :
>
> #include <string>
> #include <iostream>
>
> using namespace std::string_literals;
>
> int main(int, char **)
> {
>     std::cout << "Hello "s + "world!" << std::endl;
>     return 0;
> }
>
>
>
> file CMakeLists.txt :
>
> project(reqstd_test)
> cmake_minimum_required(VERSION 3.7)
>
> set(CMAKE_CXX_STANDARD 14)
> set(CMAKE_CXX_STANDARD_REQUIRED ON)
> set(CMAKE_CXX_EXTENSIONS OFF)
>
> add_executable(hello main.cpp)
>
>
> cmake output:
> wdobbe@linux-wdobbe:/data/generated/cmake_std_req/build> cmake ..
> -- The C compiler identification is GNU 4.8.5
> -- The CXX compiler identification is GNU 4.8.5
> -- Check for working C compiler: /usr/bin/cc
> -- Check for working C compiler: /usr/bin/cc -- works
> -- Detecting C compiler ABI info
> -- Detecting C compiler ABI info - done
> -- Detecting C compile features
> -- Detecting C compile features - done
> -- Check for working CXX compiler: /usr/bin/c++
> -- Check for working CXX compiler: /usr/bin/c++ -- works
> -- Detecting CXX compiler ABI info
> -- Detecting CXX compiler ABI info - done
> -- Detecting CXX compile features
> -- Detecting CXX compile features - done
> -- Configuring done
> -- Generating done
> -- Build files have been written to: /data/generated/cmake_std_req/build
> wdobbe@linux-wdobbe:/data/generated/cmake_std_req/build>
>
>
> make output:
> wdobbe@linux-wdobbe:/data/generated/cmake_std_req/build> make VERBOSE=Y
> /home/wdobbe/install/cmake-3.8.0-rc2-Linux-x86_64/bin/cmake
> -H/data/generated/cmake_std_req -B/data/generated/cmake_std_req/build
> --check-build-system CMakeFiles/Makefile.cmake 0
> <cut>
> [ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
> /usr/bin/c++    -std=c++1y -o CMakeFiles/hello.dir/main.cpp.o -c
> /data/generated/cmake_std_req/main.cpp
> /data/generated/cmake_std_req/main.cpp:4:22: error: ‘string_literals’ is not
> a namespace-name
>  using namespace std::string_literals;
>                       ^
> /data/generated/cmake_std_req/main.cpp:4:37: error: expected namespace-name
> before ‘;’ token
>  using namespace std::string_literals;
>                                      ^
> /data/generated/cmake_std_req/main.cpp: In function ‘int main(int, char**)’:
> /data/generated/cmake_std_req/main.cpp:8:18: error: unable to find string
> literal operator ‘operator"" s’
>      std::cout << "Hello "s + "world!" << std::endl;
>                   ^
> CMakeFiles/hello.dir/build.make:62: recipe for target
> 'CMakeFiles/hello.dir/main.cpp.o' failed
> make[2]: *** [CMakeFiles/hello.dir/main.cpp.o] Error 1
> make[2]: Leaving directory '/data/generated/cmake_std_req/build'
> CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/hello.dir/all' failed
> make[1]: *** [CMakeFiles/hello.dir/all] Error 2
> make[1]: Leaving directory '/data/generated/cmake_std_req/build'
> Makefile:83: recipe for target 'all' failed
> make: *** [all] Error 2
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: CMAKE_CXX_STANDARD_REQUIRED not working in helloworld

winfried_mb2
Robert Maynard schreef op 07.03.2017 16:34:
> The issue is that GCC 4.8 c++1y does offer support for some c++14
> features ( https://gcc.gnu.org/projects/cxx-status.html#cxx14 ) and
> therefore that is why you getting a compile time failure. You could
> try using target_compile_features instead but it only verifies
> language level features and is not aware of standard library features
> such as std::string_literals.
>

Thanks for the clarification. I already tried setting the CXX_STANDARD
and CXX_STANDARD_REQUIRED target compile features directly, but that
gives the same result.
I will add an extra check for gcc >= 4.9.


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Loading...