ufk
ufk

Reputation: 32094

getting the CppServer's asio example compiled in a meson project using the cmake module

I'm trying to add CppServer from https://github.com/chronoxor/CppServer as a dependency to my cpp meson project and I'm trying to run the asio server/client example from https://github.com/chronoxor/CppServer#example-asio-service.

as first I thought that I also need to download asio library from https://think-async.com/Asio/, but then i saw that when I compile Cppserver it already compiles it as a module.. anywho i'm a bit new to meson and for some reason to add CppServer as a depdendency is a very confusion, i'll show what I have so far:

so this is what I added to my meson.build file:

cppserver_sources = files(
    'my_cppserver.cpp',
    'my_cppserver.h',
    'my_cppclient.cpp',
    'my_cppclient.h'
)

cpp_server_ext_sources = files(
'../../../extern/CppServer/modules/asio/asio/include/asio.hpp'
)


proj_root = meson.current_source_dir() + '/../../..'
cpp = meson.get_compiler('cpp')
lib_proto = cpp.find_library('proto', dirs: proj_root + '/extern/CppServer/bin')
if not lib_proto.found()
    error('Library proto not found')
endif

lib_cppserver = cpp.find_library('cppserver', dirs: proj_root + '/extern/CppServer/bin')
if not lib_cppserver.found()
    error('Library cppserver not found')
endif



cppserver_ext_includes = include_directories('../../../extern/CppServer/include','../../../extern/CppServer/modules/CppCommon/include',
    '../../../extern/CppServer/modules/asio/asio/include')


# Create a static library including the OpenGL dependency
cppserver_feature_lib = static_library(
    'cppserver_feature',
    [cppserver_sources, cpp_server_ext_sources],
    dependencies: [lib_proto, lib_cppserver],
    include_directories: cppserver_ext_includes
)

cppserver_dep = declare_dependency(link_with: cppserver_feature_lib, include_directories: cppserver_ext_includes)

again.. I'm really lost here I'm just trying things to see how to get the example compiled with meson, adding cpp_server_ext_sources didn't really change anything, again i was just trying stuff.

my code is simply the client/server example from the github repo, i don't think the code got nothing to do with it, it includes asio, and for that i get tons of undefined references, i truncated it:

====================[ Build | music_os ]========================================
meson compile music_os -j 22
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /home/ufk/.local/share/JetBrains/Toolbox/apps/clion-nova/bin/ninja/linux/x64/ninja -j 22 src/server/music_os
[1/1] Linking target src/server/music_os
FAILED: src/server/music_os 
c++  -o src/server/music_os src/server/music_os.p/music_os_server.cpp.o src/server/music_os.p/main.cpp.o -Wl,--as-needed -Wl,--no-undefined -Wl,--start-group src/features/cppserver/libcppserver_feature.a /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libproto.a /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a -Wl,--end-group
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): warning: relocation against `_ZTIN4asio9execution12bad_executorE' in read-only section `.text.unlikely'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(service.cpp.o): in function `asio::detail::completion_handler<CppServer::Asio::Service::Start(bool)::{lambda()#1}, asio::io_context::basic_executor_type<std::allocator<void>, 0ul> >::do_complete(void*, asio::detail::scheduler_operation*, std::error_code const&, unsigned long)':
service.cpp:(.text+0x1a7): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: service.cpp:(.text+0x2bb): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(service.cpp.o): in function `asio::detail::completion_handler<CppServer::Asio::Service::Stop()::{lambda()#1}, asio::io_context::basic_executor_type<std::allocator<void>, 0ul> >::do_complete(void*, asio::detail::scheduler_operation*, std::error_code const&, unsigned long)':
service.cpp:(.text+0x437): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: service.cpp:(.text+0x494): undefined reference to `asio::io_context::stop()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: service.cpp:(.text+0x56c): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(service.cpp.o): in function `CppServer::Asio::Service::Restart()':
service.cpp:(.text+0x74c): undefined reference to `asio::io_context::io_context()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: service.cpp:(.text+0x8a6): undefined reference to `asio::detail::service_registry::do_use_service(asio::execution_context::service::key const&, asio::execution_context::service* (*)(void*), void*)'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: service.cpp:(.text+0x8b6): undefined reference to `asio::detail::strand_service::construct(asio::detail::strand_service::strand_impl*&)'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(service.cpp.o): in function `CppServer::Asio::Service::Stop()':
service.cpp:(.text+0xa44): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: service.cpp:(.text+0xaf4): undefined reference to `asio::detail::strand_service::do_post(asio::detail::strand_service::strand_impl*&, asio::detail::scheduler_operation*, bool)'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: service.cpp:(.text+0xb71): undefined reference to `CppCommon::Thread::Yield()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: service.cpp:(.text+0xc38): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: service.cpp:(.text+0xce6): undefined reference to `asio::detail::scheduler::post_immediate_completion(asio::detail::scheduler_operation*, bool)'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(service.cpp.o): in function `CppServer::Asio::Service::Start(bool)':
service.cpp:(.text+0xfe0): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::reactive_socket_sendto_op<asio::const_buffers_1, asio::ip::basic_endpoint<asio::ip::udp>, CppServer::Asio::UDPServer::Send(asio::ip::basic_endpoint<asio::ip::udp> const&, void const*, unsigned long, CppCommon::Timespan const&)::{lambda(std::error_code, unsigned long)#1}, asio::any_io_executor>::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x135): undefined reference to `asio::any_io_executor::~any_io_executor()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: udp_server.cpp:(.text.unlikely+0x149): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::wait_handler<CppServer::Asio::UDPServer::Receive(asio::ip::basic_endpoint<asio::ip::udp>&, void*, unsigned long, CppCommon::Timespan const&)::{lambda(std::error_code const&)#1}, asio::any_io_executor>::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x19e): undefined reference to `asio::any_io_executor::~any_io_executor()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: udp_server.cpp:(.text.unlikely+0x1b2): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::reactive_socket_recvfrom_op<asio::mutable_buffers_1, asio::ip::basic_endpoint<asio::ip::udp>, CppServer::Asio::UDPServer::Receive(asio::ip::basic_endpoint<asio::ip::udp>&, void*, unsigned long, CppCommon::Timespan const&)::{lambda(std::error_code, unsigned long)#1}, asio::any_io_executor>::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x204): undefined reference to `asio::any_io_executor::~any_io_executor()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: udp_server.cpp:(.text.unlikely+0x218): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::completion_handler<CppServer::Asio::UDPServer::Stop()::{lambda()#1}, asio::io_context::basic_executor_type<std::allocator<void>, 0ul> >::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x2d9): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::completion_handler<CppServer::Asio::UDPServer::Start()::{lambda()#1}, asio::io_context::basic_executor_type<std::allocator<void>, 0ul> >::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x499): undefined reference to `asio::detail::thread_context::top_of_thread_call_stack()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::reactive_socket_recvfrom_op<asio::mutable_buffers_1, asio::ip::basic_endpoint<asio::ip::udp>, CppServer::Asio::AllocateHandler<CppServer::Asio::UDPServer::TryReceive()::{lambda(std::error_code, unsigned long)#1}>, asio::any_io_executor>::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x5f0): undefined reference to `asio::any_io_executor::~any_io_executor()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::reactive_socket_sendto_op<asio::mutable_buffers_1, asio::ip::basic_endpoint<asio::ip::udp>, CppServer::Asio::AllocateHandler<CppServer::Asio::UDPServer::SendAsync(asio::ip::basic_endpoint<asio::ip::udp> const&, void const*, unsigned long)::{lambda(std::error_code, unsigned long)#1}>, asio::any_io_executor>::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x6a4): undefined reference to `asio::any_io_executor::~any_io_executor()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::completion_handler<asio::detail::binder2<asio::executor_binder<CppServer::Asio::AllocateHandler<CppServer::Asio::UDPServer::TryReceive()::{lambda(std::error_code, unsigned long)#1}>, asio::io_context::strand>, std::error_code, unsigned long>, asio::io_context::basic_executor_type<std::allocator<void>, 0ul> >::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x775): undefined reference to `asio::detail::scheduler::stop()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::completion_handler<asio::detail::binder2<asio::executor_binder<CppServer::Asio::AllocateHandler<CppServer::Asio::UDPServer::SendAsync(asio::ip::basic_endpoint<asio::ip::udp> const&, void const*, unsigned long)::{lambda(std::error_code, unsigned long)#1}>, asio::io_context::strand>, std::error_code, unsigned long>, asio::io_context::basic_executor_type<std::allocator<void>, 0ul> >::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x843): undefined reference to `asio::detail::scheduler::stop()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/src/features/cppserver/../../../extern/CppServer/bin/libcppserver.a(udp_server.cpp.o): in function `asio::detail::reactive_socket_sendto_op<asio::mutable_buffers_1, asio::ip::basic_endpoint<asio::ip::udp>, asio::executor_binder<CppServer::Asio::AllocateHandler<CppServer::Asio::UDPServer::SendAsync(asio::ip::basic_endpoint<asio::ip::udp> const&, void const*, unsigned long)::{lambda(std::error_code, unsigned long)#1}>, asio::io_context::strand>, asio::any_io_executor>::ptr::reset()':
udp_server.cpp:(.text.unlikely+0x917): undefined reference to `asio::detail::scheduler::stop()'
...
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: src/features/cppserver/libcppserver_feature.a.p/my_cppserver.cpp.o: in function `__static_initialization_and_destruction_0()':
/home/ufk/projects/tux-in/tux-music-studio-server-cpp/buildDir/../extern/CppServer/modules/asio/asio/include/asio/ssl/error.hpp:37:(.text+0x3ed): undefined reference to `asio::error::get_ssl_category()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/buildDir/../extern/CppServer/modules/asio/asio/include/asio/ssl/error.hpp:74:(.text+0x3f9): undefined reference to `asio::ssl::error::get_stream_category()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: src/features/cppserver/libcppserver_feature.a.p/my_cppserver.cpp.o: in function `asio::detail::io_object_impl<asio::detail::reactive_socket_service<asio::ip::udp>, asio::any_io_executor>::~io_object_impl()':
/home/ufk/projects/tux-in/tux-music-studio-server-cpp/buildDir/../extern/CppServer/modules/asio/asio/include/asio/detail/io_object_impl.hpp:94:(.text._ZN4asio6detail14io_object_implINS0_23reactive_socket_serviceINS_2ip3udpEEENS_15any_io_executorEED2Ev[_ZN4asio6detail14io_object_implINS0_23reactive_socket_serviceINS_2ip3udpEEENS_15any_io_executorEED5Ev]+0x26): undefined reference to `asio::detail::reactive_socket_service_base::destroy(asio::detail::reactive_socket_service_base::base_implementation_type&)'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: /home/ufk/projects/tux-in/tux-music-studio-server-cpp/buildDir/../extern/CppServer/modules/asio/asio/include/asio/detail/io_object_impl.hpp:95:(.text._ZN4asio6detail14io_object_implINS0_23reactive_socket_serviceINS_2ip3udpEEENS_15any_io_executorEED2Ev[_ZN4asio6detail14io_object_implINS0_23reactive_socket_serviceINS_2ip3udpEEENS_15any_io_executorEED5Ev]+0x36): undefined reference to `asio::any_io_executor::~any_io_executor()'
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Process finished with exit code 1

so as you can see here i cloned CppServer git repo to /extern/CppServer and it's compiled. i also added /extern/asio for the asio code from https://think-async.com/Asio/ but again i saw it in the modules of the CppServer so it's not needed.

i have no idea how to continue, any information regarding this issue would be greatly appreciated.

my current setup is on gentoo linux using gcc 13.

update

thanks to @ElvisOric i understood that there is a cmake module for meson.

i use meson 1.3.1 and cmake 3.28.1 on gentoo linux.

so instead of having CppServer in /extern i created a CppServer.wrap file in subprojects:

[wrap-git]
url = https://github.com/chronoxor/CppServer
revision = head
depth = 1
buildsystem = cmake

[provide]
dependency_names = asio, cppserver, proto

and in my main meson file i configured this:

cmake = import('cmake') cpp_server_subproject = cmake.subproject('CppServer') lib_proto = cpp_server_subproject.dependency('proto') lib_asio = cpp_server_subproject.dependency('asio') lib_cppserver = cpp_server_subproject.dependency('cppserver')

and in my meson file inside one of the directories in src directory that actually uses that library:

subproject_dir = '../../../subprojects/CppServer'

cppserver_ext_includes = include_directories(subproject_dir + '/include', subproject_dir + '/modules/CppCommon/include',
                                             subproject_dir + '/modules/asio/asio/include')


if not lib_proto.found()
   error('missing lib_proto')
endif

if not lib_cppserver.found()
   error('missing lib_cppserver')
endif

if not lib_asio.found()
   error('missing asio')
endif


# Create a static library including the OpenGL dependency
cppserver_feature_lib = static_library(
    'cppserver_feature',
    [cppserver_sources],
    dependencies: [lib_proto, lib_cppserver],
    include_directories: cppserver_ext_includes
)

cppserver_dep = declare_dependency(link_with: cppserver_feature_lib, include_directories: cppserver_ext_includes)

ok i understand that building the include directories like that is forbidden and i should include something from the CMakelists.txt of the CppServer project, still investigating. with current configration i still get undefined reference on asio related stuff.

Upvotes: 0

Views: 227

Answers (1)

ufk
ufk

Reputation: 32094

ok that was a fun ride, so this is how i got it to work.

so this is my final CppServer.wrap:

[wrap-git]
url = https://github.com/chronoxor/CppServer
revision = head
depth = 1
buildsystem = cmake

[provide]
dependency_names = asio, cppserver, proto, cppcommon, fmt

so if someone will clone the project, he will first need to do: meson subprojects download to download the subproject, the developer should have python installed with gil pip package, he should go to subprojects/CppServer and run gil update, i didn't find a way to resolve it automatically with meson, so i will just specify that in readme and create a batch file.

i'm getting all the libraries dependencies with:

cmake = import('cmake')
cpp_server_subproject = cmake.subproject('CppServer')
lib_proto = cpp_server_subproject.dependency('proto')
lib_asio = cpp_server_subproject.dependency('asio')
lib_fmt = cpp_server_subproject.dependency('fmt')
lib_cppserver = cpp_server_subproject.dependency('cppserver')
lib_cppcommon = cpp_server_subproject.dependency('cppcommon')

and in the main meson.build file i defined the variables cppserver_ext_includes to inlcude the directories that i need in order to compile the asio example:

subproject_dir = 'subprojects/CppServer'

cppserver_ext_includes = include_directories(subproject_dir + '/include', subproject_dir + '/modules/CppCommon/include',
                                             subproject_dir + '/modules/asio/asio/include')

in my case i create a library to used in the project so i create a static lib:

cppserver_feature_lib = static_library(
    'cppserver_feature',
    [cppserver_sources],
    dependencies: [lib_proto, lib_cppserver, lib_asio, lib_cppcommon, lib_fmt],
    include_directories: cppserver_ext_includes
)

and that's it, now the examples work.

this answer works, i'm not sure it's the best, i'm pretty new to meson, but it will do :)

Upvotes: -1

Related Questions