Skip to content

Unable to use cppgraphqlgen #170

Closed
Closed

Description

@kriegljan

I try to use the cppgrahqlgen library in my own project. Therefore, I created the project in a new directory using (for a starting point) the files from the star wars sample.

generated/
  DroidObject.cpp          
  DroidObject.h         
  HumanObject.cpp      
  HumanObject.h         
  learn_schema_files
  MutationObject.h
  MutationObject.cpp
  QueryObject.h
  QueryObject.cpp
  ReviewObject.h
  ReviewObject.cpp
  StarWarsObjects.h
  StarWarsSchema.h
  StarWarsSchema.cpp
src/
  DroidData.cpp              
  DroidData.h        
  HeroData.h         
  HumanData.cpp
  HumanData.h  
  MutationData.cpp
  MutationData.h 
  QueryData.cpp
  QueryData.h  
  ReviewData.cpp 
  ReviewData.h
  sample.cpp
  StarWarsData.cpp
  StarWarsData.h
CMakelists.txt (see below)

I tried to adapt the CMakelists.txt from the samples, but I had to use link_directories to link to graphqlintrospection lib and so on.

cmake_minimum_required(VERSION 3.8.2)
project(graphql_example)

set (CMAKE_CXX_STANDARD 17)

include_directories(/home/jsk/third_party_libs/vcpkg/installed/x64-linux/include)
link_directories(/home/jsk/third_party_libs/vcpkg/installed/x64-linux/lib)

# learnschema
set(LEARN_SCHEMA_PATHS "")
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/generated/learn_schema_files LEARN_SCHEMA_FILES)
foreach(CPP_FILE IN LISTS LEARN_SCHEMA_FILES)
  list(APPEND LEARN_SCHEMA_PATHS "${CMAKE_CURRENT_SOURCE_DIR}/generated/${CPP_FILE}")
endforeach(CPP_FILE)

add_library(learnschema STATIC ${LEARN_SCHEMA_PATHS})
#   target_link_libraries(learnschema PUBLIC graphqlintrospection) # build fails
target_link_libraries(learnschema PUBLIC graphqlintrospection graphqljson graphqlresponse graphqlservice graphqlpeg graphqlclient pthread stdc++fs)
target_include_directories(learnschema PUBLIC
#   ${CMAKE_CURRENT_SOURCE_DIR}/../include
#   ${CMAKE_CURRENT_SOURCE_DIR}/../PEGTL/include
  /home/jsk/third_party_libs/cppgraphqlgen/PEGTL/include
  ${CMAKE_CURRENT_SOURCE_DIR}/generated)

# star_wars
add_library(star_wars STATIC
  src/DroidData.cpp
  src/HumanData.cpp
  src/QueryData.cpp
  src/ReviewData.cpp
  src/MutationData.cpp
  src/StarWarsData.cpp)
target_link_libraries(star_wars PUBLIC learnschema)
target_include_directories(star_wars PUBLIC star_wars)

# learn_star_wars
add_executable(learn_star_wars src/sample.cpp)
target_link_libraries(learn_star_wars PRIVATE
  star_wars
  graphqljson)
target_include_directories(learn_star_wars PRIVATE
#   ${CMAKE_CURRENT_SOURCE_DIR}/../include
  /home/jsk/third_party_libs/vcpkg/installed/x64-linux/include
#   ${CMAKE_CURRENT_SOURCE_DIR}/../PEGTL/include
  /home/jsk/third_party_libs/cppgraphqlgen/PEGTL/include
)

I installed cppgraphqlgen using vcpgk in /home/jsk/third_party_libs/vcpkg. I also have the source in /home/danneboom/third_party_libs/cppgraphqlgen.

The commands I use to build the project:

cd build
cmake .. -DCMAKE_C_COMPILER=gcc-9 -DCMAKE_CXX_COMPILER=g++-9
cmake --build .

This way I am able to build. But something is wrong, since when I execute learn_star_wars, I get a segmentation fault:

Starting program: /home/jsk/graphql_example/build/learn_star_wars 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x000055555565f54e in graphql::schema::Field::Make(std::basic_string_view<char, std::char_traits<char> >, std::basic_string_view<char, std::char_traits<char> >, std::optional<std::basic_string_view<char, std::char_traits<char> > >, std::weak_ptr<graphql::schema::BaseType const>, std::vector<std::shared_ptr<graphql::schema::InputValue const>, std::allocator<std::shared_ptr<graphql::schema::InputValue const> > >&&) ()

What is the correct way to use cppgraphqlgen in a custom project?
I'm running Ubuntu 18.4 using GNU 9.4.0 and cmake 3.10.2

Activity

  1. wravery commented on Nov 19, 2021

    @wravery
    Contributor

    I think the segfault is probably caused by a mismatch in the C++ standard libraries, or maybe some other ABI mismatch due to compiler flags. Since you're using the version which you built with vcpkg, try adding -DCMAKE_TOOLCHAIN_FILE=/home/jsk/third_party_libs/vcpkg/scripts/buildsystems/vcpkg.cmake to your first cmake command line. You may need to delete the build directory first and re-create it, I'm not sure cmake will pay enough attention to the new variable in an already configured directory.

    If you use the vcpkg toolchain file, you should be able to put back the commented-out lines that didn't build before, and comment-out the lines that you added to replace them.

    If you'd rather build cppgraphqlgen from source as part of your build, it should also support add_subdirectory. You might need to put it in a sub-directory of your main project, I'm not sure if you can point to your copy in /home/danneboom/third_party_libs/cppgraphqlgen if it's not a sub-directory of your project. Also, if you're not using vcpkg, you'll need to make sure you have the other dependencies like PEGTL (included as a Git sub-module in cppgraphqlgen, but may not be cloned by default). Most targets have an alternate way to install boost libraries, if you install a separate package, cmake needs to be able to find them for the command line handling in schemagen and clientgen. It also needs gtest/googletest if you are building unit tests.

  2. kriegljan commented on Nov 22, 2021

    @kriegljan
    Author

    I tried deleting the build folder and build again with the toolchain file, only linking to graphqlintrospection:

    $ cmake .. -DCMAKE_C_COMPILER=gcc-9 -DCMAKE_CXX_COMPILER=g++-9 -DCMAKE_TOOLCHAIN_FILE=/home/jsk/third_party_libs/vcpkg/scripts/buildsystems/vcpkg.cmake

    The output is attached in output.txt. I think it cannot find the implementation of the graphqlresponse library. That's why I added them explicitly to CMakelists.txt

    I attached the project as a zip file, if you want to try it by yourself.
    output.txt
    graphql_example.zip

    In general, I would like to use cppgraphqlgen like a usual system library, which means (in my understanding) that I can use PackageConfig to find the package and then link the headers and the libraries

    find_package(cppgraphqlgen)
    (...)
    include_directories(${cppgraphqlgen_INCLUDE})
    target_link_libraries(mytarget ${cppgraphqlgen_LIBRARIES})

    Is there any way to use cppgraphqlgen like that? Install the library system-wide and provide a pkgconfig?

  3. wravery commented on Nov 23, 2021

    @wravery
    Contributor

    Is there any way to use cppgraphqlgen like that? Install the library system-wide and provide a pkgconfig?

    Yes, although I don't think it's the same thing as pkgconfig. I'm not that familiar with pkgconfig, but I think it uses .pc files and integrates with other things besides cmake.

    Check out the Command Line on any platform section of the README.

    The install target for cmake creates .cmake files which enable find_package. Among other things, that config file for cmake should set the include and library directories and specify the dependencies between targets, so you only need to link with cppgraphqlgen::graphqlintrospection for it to pick up everything else.

  4. added
    question
    on Nov 27, 2021
  5. wravery commented on Nov 27, 2021

    @wravery
    Contributor

    @ipa-jsk: I'll leave this open a couple more days in case you're still stuck.

  6. kriegljan commented on Nov 29, 2021

    @kriegljan
    Author

    Thanks, I'm still stuck indeed ;)

    According to your suggestion, I tried to build cppgraphqlgen from source like described in the readme.

     cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc-9 -DCMAKE_CXX_COMPILER=g++-9 ..
     sudo cmake --build . --target install

    But there is some error with std::experimental::filesystem:

    Scanning dependencies of target client_benchmark
    [ 39%] Building CXX object samples/CMakeFiles/client_benchmark.dir/client/BenchmarkClient.cpp.o
    [ 40%] Building CXX object samples/CMakeFiles/client_benchmark.dir/client/benchmark.cpp.o
    [ 41%] Linking CXX executable client_benchmark
    ../src/libgraphqlpeg.a(SyntaxTree.cpp.o): In function `std::experimental::filesystem::v1::__cxx11::filesystem_error::filesystem_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::experimental::filesystem::v1::__cxx11::path const&, std::error_code)':
    SyntaxTree.cpp:(.text._ZNSt12experimental10filesystem2v17__cxx1116filesystem_errorC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS2_4pathESt10error_code[_ZNSt12experimental10filesystem2v17__cxx1116filesystem_errorC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS2_4pathESt10error_code]+0x288): undefined reference to `std::experimental::filesystem::v1::__cxx11::filesystem_error::_M_gen_what()'
    collect2: error: ld returned 1 exit status
    samples/CMakeFiles/client_benchmark.dir/build.make:128: recipe for target 'samples/client_benchmark' failed
    make[2]: *** [samples/client_benchmark] Error 1
    CMakeFiles/Makefile2:1580: recipe for target 'samples/CMakeFiles/client_benchmark.dir/all' failed
    make[1]: *** [samples/CMakeFiles/client_benchmark.dir/all] Error 2
    Makefile:140: recipe for target 'all' failed
    make: *** [all] Error 2
    

    This error does not occur when building without -DCMAKE_BUILD_TYPE=Release. Any idea whats going wrong?

    Edit: Okay, I need to run
    cmake --build . before running sudo cmake --build . --target install

  7. kriegljan commented on Nov 29, 2021

    @kriegljan
    Author

    But also with cppgraphqlgen built from source with sudo and --target install, I am not able to build:

    $ cmake --build .
    -- Configuring done
    CMake Error at CMakeLists.txt:47 (add_executable):
      Target "learn_star_wars" links to target "taocpp::pegtl" but the target was
      not found.  Perhaps a find_package() call is missing for an IMPORTED
      target, or an ALIAS target is missing?
    
    
    CMake Error at CMakeLists.txt:36 (add_library):
      Target "star_wars" links to target "taocpp::pegtl" but the target was not
      found.  Perhaps a find_package() call is missing for an IMPORTED target, or
      an ALIAS target is missing?
    
    
    CMake Error at CMakeLists.txt:20 (add_library):
      Target "learnschema" links to target "taocpp::pegtl" but the target was not
      found.  Perhaps a find_package() call is missing for an IMPORTED target, or
      an ALIAS target is missing?
    
    
    -- Generating done
    -- Build files have been written to: /home/jsk/graphql_example/build
    Makefile:564: recipe for target 'cmake_check_build_system' failed
    make: *** [cmake_check_build_system] Error 1
    
    

    CMakelists.txt

    Do I still miss some find_packages/includes/linking/dependencies ?

  8. wravery commented on Nov 29, 2021

    @wravery
    Contributor

    But also with cppgraphqlgen built from source with sudo and --target install, I am not able to build:

    $ cmake --build .
    -- Configuring done
    CMake Error at CMakeLists.txt:47 (add_executable):
      Target "learn_star_wars" links to target "taocpp::pegtl" but the target was
      not found.  Perhaps a find_package() call is missing for an IMPORTED
      target, or an ALIAS target is missing?
    

    CMakelists.txt

    Do I still miss some find_packages/includes/linking/dependencies ?

    Yes, that means it couldn't find PEGTL. If you have cloned the cppgraphqlgen repo, you can pull it in as a Git submodule using the commands:

    > git submodule init
    > git submodule update

    If you are using a source archive/tar ball, then you probably need to perform this process of building and installing https://github.com/taocpp/PEGTL with cmake first, and it should put its own .cmake configuration files where find_package in cppgraphqlgen can find them.

    This is all much easier with vcpkg, BTW. That's why I always recommend trying that first. ;)

  9. kriegljan commented on Dec 2, 2021

    @kriegljan
    Author

    I finally figured it out: a find_package(pegtl) command was missing. So, finally, the workflow which worked for me is

    • clone cppgraphqlgen repo
    • load git submodules
    • in CMakelists.txt, set GRAPHQL_BUILD_SCHEMAGEN and GRAPHQL_BUILD_CLIENTGEN to OFF (I was not able to build due to boost version conflicts)
    • cmake .. -DCMAKE_C_COMPILER=gcc-9 -DCMAKE_CXX_COMPILER=g++-9
    • cmake --build .
    • cmake -DCMAKE_BUILD_TYPE=Release ..
    • cmake --build . --target install

    Then, the attached CMakeLists.txt shows how to link to cppgraphqlgen.

    Thanks for your support!

    This is all much easier with vcpkg, BTW. That's why I always recommend trying that first. ;)

    Yeah I tried that first, but it didnt work either, maybe its an ubuntu issue (I think the integrate command didn't work properly).

  10. wravery commented on Dec 2, 2021

    @wravery
    Contributor
    • in CMakelists.txt, set GRAPHQL_BUILD_SCHEMAGEN and GRAPHQL_BUILD_CLIENTGEN to OFF (I was not able to build due to boost version conflicts)

    You'll probably want to get GRAPHQL_BUILD_SCHEMAGEN working at some point, otherwise you can't build your own schemas. If you can get vcpkg to install the cppgraphqlgen package locally, even if you can't link or build your own project against it, it should include a schemagen binary that you can use instead of building it yourself.

    • cmake .. -DCMAKE_C_COMPILER=gcc-9 -DCMAKE_CXX_COMPILER=g++-9
    • cmake --build .
    • cmake -DCMAKE_BUILD_TYPE=Release ..
    • cmake --build . --target install

    Then, the attached CMakeLists.txt shows how to link to cppgraphqlgen.

    Yup, that looks much simpler, and it's taking advantage of the cppgraphqlgen-config.cmake file that this project installs.

    This is all much easier with vcpkg, BTW. That's why I always recommend trying that first. ;)

    Yeah I tried that first, but it didnt work either, maybe its an ubuntu issue (I think the integrate command didn't work properly).

    I think the integrate command may be unnecessary on Linux, I think it just adds a dot-file somewhere in the home directory that tools which know about vcpkg could use to find it. Either way, you usually need to manually specify the -DCMAKE_TOOLCHAIN_FILE=... parameter to cmake, because I think the only tools that do that automatically based on vcpkg integrate are Visual Studio and VS Code on Windows.

    Anyway, I'm glad you're unblocked. 😄 I'll go ahead and close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions