pabloariasal / modern-cmake-sample

Example library that shows best practices and proper usage of CMake by using targets
MIT License
667 stars 70 forks source link

Build fails with boost 1.55 and gcc #3

Open teeks99 opened 6 years ago

teeks99 commented 6 years ago

Does the version of boost you used have some patch applied to optional? I see this error.

tomkent@frink:~/tmp/modern-cmake-sample/libjsonutils/build$ make
[ 12%] Building CXX object CMakeFiles/jsonutils.dir/src/json_utils.cpp.o
In file included from /home/tomkent/tmp/modern-cmake-sample/libjsonutils/include/jsonutils/json_utils.h:6:0,
                 from /home/tomkent/tmp/modern-cmake-sample/libjsonutils/src/json_utils.cpp:1:
/home/tomkent/tmp/boost_1_55_0/boost/optional/optional.hpp: In instantiation of ‘void boost::optional_detail::optional_base<T>::construct(boost::optional_detail::optional_base<T>::argument_type) [with T = rapidjson::GenericDocument<rapidjson::UTF8<> >; boost::optional_detail::optional_base<T>::argument_type = const rapidjson::GenericDocument<rapidjson::UTF8<> >&]’:
/home/tomkent/tmp/boost_1_55_0/boost/optional/optional.hpp:230:16:   required from ‘boost::optional_detail::optional_base<T>::optional_base(boost::optional_detail::optional_base<T>::argument_type) [with T = rapidjson::GenericDocument<rapidjson::UTF8<> >; boost::optional_detail::optional_base<T>::argument_type = const rapidjson::GenericDocument<rapidjson::UTF8<> >&]’
/home/tomkent/tmp/boost_1_55_0/boost/optional/optional.hpp:526:46:   required from ‘boost::optional<T>::optional(boost::optional<T>::argument_type) [with T = rapidjson::GenericDocument<rapidjson::UTF8<> >; boost::optional<T>::argument_type = const rapidjson::GenericDocument<rapidjson::UTF8<> >&]’
/home/tomkent/tmp/modern-cmake-sample/libjsonutils/src/json_utils.cpp:20:16:   required from here
/home/tomkent/tmp/rapidjson/include/rapidjson/document.h:1902:5: error: ‘rapidjson::GenericDocument<Encoding, Allocator, StackAllocator>::GenericDocument(const rapidjson::GenericDocument<Encoding, Allocator, StackAllocator>&) [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; StackAllocator = rapidjson::CrtAllocator]’ is private
     GenericDocument(const GenericDocument&);
     ^
In file included from /home/tomkent/tmp/boost_1_55_0/boost/optional.hpp:15:0,
                 from /home/tomkent/tmp/modern-cmake-sample/libjsonutils/include/jsonutils/json_utils.h:5,
                 from /home/tomkent/tmp/modern-cmake-sample/libjsonutils/src/json_utils.cpp:1:
/home/tomkent/tmp/boost_1_55_0/boost/optional/optional.hpp:346:8: error: within this context
        new (m_storage.address()) internal_type(val) ;
        ^
CMakeFiles/jsonutils.dir/build.make:62: recipe for target 'CMakeFiles/jsonutils.dir/src/json_utils.cpp.o' failed
make[2]: *** [CMakeFiles/jsonutils.dir/src/json_utils.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/jsonutils.dir/all' failed
make[1]: *** [CMakeFiles/jsonutils.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
tomkent@frink:~/tmp/modern-cmake-sample/libjsonutils/build$ 
tomkent@frink:~/tmp/modern-cmake-sample/libjsonutils/build$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
pabloariasal commented 6 years ago

The error is not related to boost at all, but to rapidjson. How did you install rapidjson? Can you test with the official version 1.1.0 and see if the problem persists?

teeks99 commented 6 years ago

That happens when I clone the rapidjson repo from https://github.com/Tencent/rapidjson.git then checkout v1.1.0.

teeks99 commented 6 years ago

I also tried with ubuntu xenial's default version of boost (1.58), it has a different issue.

(Trivial commands below have output clipped)

# Start with a clean docker image
docker run --rm -it ubuntu:xenial /bin/bash

apt update
# This installs gcc 5, boost 1.58, cmake 3.5.1
apt install build-essential libboost-regex-dev git cmake
mkdir src
cd src
# Get v1.1 of rapidjson
git clone https://github.com/Tencent/rapidjson.git
cd rapidjson
git checkout v1.1.0
cd ..
# Get the example 
git clone https://github.com/pabloariasal/modern-cmake-sample.git
cd modern-cmake-sample
mkdir build
cd build
export CMAKE_PREFIX_PATH=/src/rapidjson/

$ cmake ../libjsonutils/
-- The CXX compiler identification is GNU 5.4.0
-- 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
-- Boost version: 1.58.0
-- Found the following Boost libraries:
--   regex
-- Could NOT find PkgConfig (missing:  PKG_CONFIG_EXECUTABLE) 
-- Found RapidJSON: /src/rapidjson/include/rapidjson (Required is at least version "1.0") 
-- Configuring done
-- Generating done
-- Build files have been written to: /src/modern-cmake-sample/build/googletest-download
Scanning dependencies of target googletest
[ 11%] Creating directories for 'googletest'
[ 22%] Performing download step (git clone) for 'googletest'
Cloning into 'googletest-src'...
Already on 'master'
Your branch is up-to-date with 'origin/master'.
[ 33%] No patch step for 'googletest'
[ 44%] Performing update step for 'googletest'
Current branch master is up to date.
[ 55%] No configure step for 'googletest'
[ 66%] No build step for 'googletest'
[ 77%] No install step for 'googletest'
[ 88%] No test step for 'googletest'
[100%] Completed 'googletest'
[100%] Built target googletest
-- The C compiler identification is GNU 5.4.0
-- 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
-- Could NOT find PythonInterp (missing:  PYTHON_EXECUTABLE) 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /src/modern-cmake-sample/build

$ cmake --build .
Scanning dependencies of target jsonutils
[ 12%] Building CXX object CMakeFiles/jsonutils.dir/src/json_utils.cpp.o
[ 25%] Linking CXX static library libjsonutils.a
[ 25%] Built target jsonutils
Scanning dependencies of target gtest
[ 37%] Building CXX object googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 50%] Linking CXX static library libgtest.a
[ 50%] Built target gtest
Scanning dependencies of target gtest_main
[ 62%] Building CXX object googletest-build/googlemock/gtest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[ 75%] Linking CXX static library libgtest_main.a
[ 75%] Built target gtest_main
Scanning dependencies of target json_utils_test
[ 87%] Building CXX object test/CMakeFiles/json_utils_test.dir/src/main.cpp.o
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp: In member function 'virtual void JsonUtilsTest_loadFromFile_Test::TestBody()':
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:6:10: error: 'doc' does not name a type
     auto doc = jsonutils::loadJson("/home/pablo/Documents/test.json");
          ^
In file included from /src/modern-cmake-sample/build/googletest-src/googletest/include/gtest/gtest.h:58:0,
                 from /src/modern-cmake-sample/libjsonutils/test/src/main.cpp:1:
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:8:17: error: 'doc' was not declared in this scope
     ASSERT_TRUE(doc);
                 ^
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:9:17: error: 'doc' was not declared in this scope
     ASSERT_TRUE(doc->HasMember("source"));
                 ^
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:10:10: error: 'source' does not name a type
     auto source = (*doc)["source"].GetString();
          ^
In file included from /src/modern-cmake-sample/build/googletest-src/googletest/include/gtest/gtest.h:360:0,
                 from /src/modern-cmake-sample/libjsonutils/test/src/main.cpp:1:
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:11:26: error: 'source' was not declared in this scope
     ASSERT_STREQ("file", source);
                          ^
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp: In member function 'virtual void JsonUtilsTest_loadFromUrl_Test::TestBody()':
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:16:10: error: 'doc' does not name a type
     auto doc = jsonutils::loadJson("https://books.com/novels");
          ^
In file included from /src/modern-cmake-sample/build/googletest-src/googletest/include/gtest/gtest.h:58:0,
                 from /src/modern-cmake-sample/libjsonutils/test/src/main.cpp:1:
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:18:17: error: 'doc' was not declared in this scope
     ASSERT_TRUE(doc);
                 ^
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:19:17: error: 'doc' was not declared in this scope
     ASSERT_TRUE(doc->HasMember("source"));
                 ^
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:20:10: error: 'source' does not name a type
     auto source = (*doc)["source"].GetString();
          ^
In file included from /src/modern-cmake-sample/build/googletest-src/googletest/include/gtest/gtest.h:360:0,
                 from /src/modern-cmake-sample/libjsonutils/test/src/main.cpp:1:
/src/modern-cmake-sample/libjsonutils/test/src/main.cpp:21:25: error: 'source' was not declared in this scope
     ASSERT_STREQ("url", source);
                         ^
test/CMakeFiles/json_utils_test.dir/build.make:62: recipe for target 'test/CMakeFiles/json_utils_test.dir/src/main.cpp.o' failed
make[2]: *** [test/CMakeFiles/json_utils_test.dir/src/main.cpp.o] Error 1
CMakeFiles/Makefile2:126: recipe for target 'test/CMakeFiles/json_utils_test.dir/all' failed
make[1]: *** [test/CMakeFiles/json_utils_test.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
pabloariasal commented 6 years ago

First of all thank you for writing the entire instructions you used. I was in fact able to reproduce your issue. The problem was that the default standard of your g++ version (5.4) is set to c++98. One of the drawbacks of my arch linux setup is that I have g++7.3, which sets the default language standard to --std=c++14. First world problems. I fixed this by telling CMake that we use auto, so it should set the language standard accordingly: target_compile_features(json_utils_test PRIVATE cxx_auto_type). Please test again.

I also discovered that the docker the image does no have vim installed per default :p first time I have to install vim on a unix system.

teeks99 commented 6 years ago

That worked fine in my docker example, using the 1.58 boost from the distro.

However, when I manually download/build boost 1.55 from sourceforge and try to use that, I get the original error in this issue. But, if I get the 1.58 from sourceforge and download/build that...it works! I'm guessing that the 1.55 that comes with Arch has some fix backported to it. I'm a bit suprised that arch is still that far back, 1.55 is from late 2013. Maybe just note this somewhere in the blog post?