Repository navigation
Closed
Labels
questionFurther information is requested
Description
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
Metadata
Metadata
Assignees
Labels
questionFurther information is requested
Activity
wravery commentedon Nov 19, 2021
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.cmaketo your firstcmakecommand line. You may need to delete the build directory first and re-create it, I'm not surecmakewill pay enough attention to the new variable in an already configured directory.If you use the
vcpkgtoolchain 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
cppgraphqlgenfrom 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/cppgraphqlgenif it's not a sub-directory of your project. Also, if you're not usingvcpkg, you'll need to make sure you have the other dependencies likePEGTL(included as a Git sub-module incppgraphqlgen, but may not be cloned by default). Most targets have an alternate way to installboostlibraries, if you install a separate package,cmakeneeds to be able to find them for the command line handling inschemagenandclientgen. It also needsgtest/googletestif you are building unit tests.kriegljan commentedon Nov 22, 2021
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.cmakeThe output is attached in
output.txt. I think it cannot find the implementation of the graphqlresponse library. That's why I added them explicitly toCMakelists.txtI 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
Is there any way to use cppgraphqlgen like that? Install the library system-wide and provide a pkgconfig?
wravery commentedon Nov 23, 2021
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
.pcfiles and integrates with other things besidescmake.Check out the Command Line on any platform section of the README.
The install target for
cmakecreates.cmakefiles which enablefind_package. Among other things, that config file forcmakeshould set the include and library directories and specify the dependencies between targets, so you only need to link withcppgraphqlgen::graphqlintrospectionfor it to pick up everything else.wravery commentedon Nov 27, 2021
@ipa-jsk: I'll leave this open a couple more days in case you're still stuck.
kriegljan commentedon Nov 29, 2021
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 installBut there is some error with std::experimental::filesystem:
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 runningsudo cmake --build . --target installkriegljan commentedon Nov 29, 2021
But also with cppgraphqlgen built from source with sudo and
--target install, I am not able to build:CMakelists.txt
Do I still miss some find_packages/includes/linking/dependencies ?
wravery commentedon Nov 29, 2021
Yes, that means it couldn't find
PEGTL. If you have cloned thecppgraphqlgenrepo, you can pull it in as a Git submodule using the commands: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
cmakefirst, and it should put its own.cmakeconfiguration files wherefind_packageincppgraphqlgencan find them.This is all much easier with
vcpkg, BTW. That's why I always recommend trying that first. ;)kriegljan commentedon Dec 2, 2021
I finally figured it out: a
find_package(pegtl)command was missing. So, finally, the workflow which worked for me isGRAPHQL_BUILD_SCHEMAGENandGRAPHQL_BUILD_CLIENTGENtoOFF(I was not able to build due to boost version conflicts)Then, the attached CMakeLists.txt shows how to link to cppgraphqlgen.
Thanks for your support!
Yeah I tried that first, but it didnt work either, maybe its an ubuntu issue (I think the
integratecommand didn't work properly).wravery commentedon Dec 2, 2021
You'll probably want to get
GRAPHQL_BUILD_SCHEMAGENworking at some point, otherwise you can't build your own schemas. If you can getvcpkgto install thecppgraphqlgenpackage locally, even if you can't link or build your own project against it, it should include aschemagenbinary that you can use instead of building it yourself.Yup, that looks much simpler, and it's taking advantage of the
cppgraphqlgen-config.cmakefile that this project installs.I think the
integratecommand may be unnecessary on Linux, I think it just adds a dot-file somewhere in the home directory that tools which know aboutvcpkgcould use to find it. Either way, you usually need to manually specify the-DCMAKE_TOOLCHAIN_FILE=...parameter tocmake, because I think the only tools that do that automatically based onvcpkg integrateare Visual Studio and VS Code on Windows.Anyway, I'm glad you're unblocked. 😄 I'll go ahead and close the issue.