Undefined reference when linking libpq in C++ project - c++

I am trying to use libpqxx (and implicitly libpq) in a C++ project.
I use vcpkg as a submodule to get my libs by setting CMAKE_TOOLCHAIN_FILE.
When I try to build, I get the following errors:
/usr/bin/ld: /home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpq.a(fe-auth.o): in function `pg_fe_sendauth':
fe-auth.c:(.text+0x4fe): undefined reference to `pg_md5_encrypt'
/usr/bin/ld: fe-auth.c:(.text+0x51b): undefined reference to `pg_md5_encrypt'
/usr/bin/ld: /home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpq.a(fe-auth.o): in function `pg_fe_getauthname':
fe-auth.c:(.text+0x838): undefined reference to `pqGetpwuid'
/usr/bin/ld: fe-auth.c:(.text+0x8a4): undefined reference to `pg_strerror_r'
/usr/bin/ld: /home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpq.a(fe-auth.o): in function `PQencryptPassword':
fe-auth.c:(.text+0x936): undefined reference to `pg_md5_encrypt'
/usr/bin/ld: /home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpq.a(fe-auth.o): in function `PQencryptPasswordConn':
fe-auth.c:(.text+0x9ed): undefined reference to `pg_md5_encrypt'
/usr/bin/ld: /home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpq.a(fe-auth-scram.o): in function `build_client_final_message':
fe-auth-scram.c:(.text+0xdb): undefined reference to `scram_SaltedPassword'
/usr/bin/ld: fe-auth-scram.c:(.text+0xee): undefined reference to `scram_ClientKey'
/usr/bin/ld: fe-auth-scram.c:(.text+0xfe): undefined reference to `scram_H'
/usr/bin/ld: fe-auth-scram.c:(.text+0x113): undefined reference to `scram_HMAC_init'
/usr/bin/ld: fe-auth-scram.c:(.text+0x12d): undefined reference to `scram_HMAC_update'
/usr/bin/ld: fe-auth-scram.c:(.text+0x141): undefined reference to `scram_HMAC_update'
/usr/bin/ld: fe-auth-scram.c:(.text+0x15b): undefined reference to `scram_HMAC_update'
/usr/bin/ld: fe-auth-scram.c:(.text+0x16f): undefined reference to `scram_HMAC_update'
/usr/bin/ld: fe-auth-scram.c:(.text+0x185): undefined reference to `scram_HMAC_update'
/usr/bin/ld: fe-auth-scram.c:(.text+0x195): undefined reference to `scram_HMAC_final'
/usr/bin/ld: fe-auth-scram.c:(.text+0x1e4): undefined reference to `pg_b64_enc_len'
/usr/bin/ld: fe-auth-scram.c:(.text+0x213): undefined reference to `pg_b64_encode'
/usr/bin/ld: fe-auth-scram.c:(.text+0x344): undefined reference to `pg_b64_enc_len'
/usr/bin/ld: fe-auth-scram.c:(.text+0x368): undefined reference to `pg_b64_encode'
...
I have broken down the linking command here:
/usr/bin/cmake -E cmake_link_script CMakeFiles/hello-pq.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/hello-pq.dir/CMakeFiles/3.16.3/CompilerIdCXX/CMakeCXXCompilerId.cpp.o CMakeFiles/hello-pq.dir/main.cpp.o
-o ../bin/hello-pq
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libssl.a
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libcrypto.a
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libpqxx-7.3.a
-lpthread
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpq.a -ldl
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../../third-party/vcpkg/scripts/buildsystems/vcpkg.cmake
CACHE STRING "Vcpkg toolchain file")
project(hello-pq)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin)
find_package(OpenSSL REQUIRED)
find_package(libpqxx CONFIG REQUIRED)
file(GLOB_RECURSE PROJECT_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
target_link_libraries(${PROJECT_NAME}
PRIVATE OpenSSL::SSL
PRIVATE OpenSSL::Crypto
PRIVATE libpqxx::pqxx)
I looked at the libpq.a from vcpkg installed dir with nm and I can see that for example, pg_md5_encrypt appears as undefined:
U pg_md5_encrypt
I don't get it, where are these missing functions like pg_md5_encrypt?
Is there another libpq... that I need to link or maybe a different version?

After some more digging around and testing I found that there are 2 more postgres static libraries that I need to link:
libpqcommon.a
libpgport.a
Additionally, the link order is also important and altough I was expecting to have to pass libs that are needed before the libs that need them, this is not the case here and it feels backwards.
Here is a working linker command:
usr/bin/c++ CMakeFiles/hello-pq.dir/src/main.cpp.o \
-o ../bin/hello-pq \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libpqxx-7.3.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpq.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpgcommon.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpgport.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libssl.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libcrypto.a \
-lpthread \
-ldl
In order to link the extra 2 static libs and also to achieve the order above, I updated the CMakeLists.txt like so:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../../third-party/vcpkg/scripts/buildsystems/vcpkg.cmake
CACHE STRING "Vcpkg toolchain file")
project(hello-pq)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin)
find_package(OpenSSL REQUIRED)
#find_package(PostgreSQL REQUIRED) # Not needed (it will duplicate the libs)
find_package(libpqxx CONFIG REQUIRED)
file(GLOB_RECURSE PROJECT_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
target_link_libraries(${PROJECT_NAME}
PRIVATE libpqxx::pqxx
PRIVATE PostgreSQL::PostgreSQL
PRIVATE OpenSSL::SSL
PRIVATE OpenSSL::Crypto)
This translates into the following linker command:
/usr/bin/c++ \
CMakeFiles/hello-pq.dir/src/main.cpp.o \
-o ../bin/hello-pq \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libpqxx-7.3.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libpq.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libssl.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/debug/lib/libcrypto.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpq.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpgport.a \
/home/user/work/cxx-playground/third-party/vcpkg/installed/x64-linux/lib/libpgcommon.a \
-ldl \
-lpthread
We can see that libpq.a appears twice in the link arguments:
once due to libpqxx::pqxx target (I assume)
once due to PostgreSQL::PostgreSQL target
which also contains the 2 missing static libs that need to be linked (libpgport.a and libpgcommon.a).
My conclusion is that this is a libpqxx bug, which should also link those 2 static libs with target_link_libraries(PUBLIC...) in the only target that it exposes (libpqxx::pqxx)

Related

Building gRPC and openssl as dependency with cmake for msys2/mingw64 fails with undefined references

I'm trying to build the grpc helloworld example with changed file tree structure and my own CMakeLists.txt with msys2/mingw64. I want to build grpc from sources as a static library using add_subdirectory and build grpc's dependencies recursevily from sources as well.
Doing that I was getting linking errors with undefined references. After some research it seems that boringssl has problems with building in msys2/mingw64 or at least the way boringssl is included in grpc's repo isn't compatible with msys2/mingw64.
Since the grpc package in msys2 is built using openssl as dependency I decided to build openssl from sources during the build and let grpc statically link to that. I hope it is enough to just clone grpc without the boringssl submodule.
I was able to succesfully link and run the example when openssl was linked dynamically to libssl.dll.a. However when I try to link to static openssl build I'm getting undefined references errors again.`grpc_test2_server.exe
This are the linking errors I'm getting:
cmd.exe /C "cd . && C:\msys64\mingw64\bin\c++.exe -g #CMakeFiles\grpc_test2_server.rsp -o grpc_test2_server.exe -Wl,--out-implib,libgrpc_test2_server.dll.a -Wl,--major-image-version,0,--minor-image-version,0 && cd ."
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_addr.o): in function `addr_strings':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_addr.c:208: undefined reference to `__imp_getnameinfo'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_addr.c:220: undefined reference to `gai_strerrorW'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_addr.o): in function `BIO_lookup_ex':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_addr.c:731: undefined reference to `gai_strerrorW'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_sock.o): in function `BIO_sock_error':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_sock.c:99: undefined reference to `__imp_getsockopt'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_sock.o): in function `BIO_gethostbyname':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_sock.c:113: undefined reference to `__imp_gethostbyname'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_sock2.o): in function `BIO_listen':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_sock2.c:217: undefined reference to `__imp_getsockopt'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_sock2.o): in function `BIO_accept_ex':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_sock2.c:290: undefined reference to `__imp_accept'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_get_prov_info':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1281: undefined reference to `__imp_CertGetCertificateContextProperty'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1289: undefined reference to `__imp_CertGetCertificateContextProperty'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_cert_get_fname':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1329: undefined reference to `__imp_CertGetCertificateContextProperty'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1335: undefined reference to `__imp_CertGetCertificateContextProperty'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_open_store':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1400: undefined reference to `__imp_CertOpenStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_list_certs':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1432: undefined reference to `__imp_CertFreeCertificateContext'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1435: undefined reference to `__imp_CertEnumCertificatesInStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1443: undefined reference to `__imp_CertCloseStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_find_cert':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1455: undefined reference to `__imp_CertFindCertificateInStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1459: undefined reference to `__imp_CertEnumCertificatesInStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_find_key':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1559: undefined reference to `__imp_CertFreeCertificateContext'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1561: undefined reference to `__imp_CertCloseStore'
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1593: undefined reference to `__imp_CertFreeCertificateC:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_load_ssl_client_cert':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1720: undefined reference to `__imp_CertEnumCertificatesInStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1740: undefined reference to `__imp_CertDuplicateCertificateContext'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1754: undefined reference to `__imp_CertFreeCertificateContext'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1756: undefined reference to `__imp_CertCloseStore'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.`
And this is my CMakeLists.txt that I'm currently using:
cmake_minimum_required(VERSION 3.24)
project(grpc_test2 C CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 17)
set(BUILD_SHARED_LIBS OFF)
set(gRPC_BUILD_SHARED_LIBS OFF)
set(gRPC_BUILD_GRPC_CSHARP_EXT OFF)
set(gRPC_BUILD_GRPC_PHP_EXT OFF)
set(gRPC_BUILD_GRPC_PYTHON_EXT OFF)
set(gRPC_BUILD_GRPC_NODE_EXT OFF)
set(gRPC_BUILD_GRPC_RUBY_EXT OFF)
set(gRPC_BUILD_GRPC_OBJC_EXT OFF)
set(gRPC_BUILD_GRPC_OBJC_PLUGIN OFF)
set(_gRPC_BASELIB_LIBRARIES libws2_32 libcrypt32)
include(ProcessorCount)
ProcessorCount(N)
set(OPENSSL_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third-party/openssl)
set(OPENSSL_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/third-party/openssl_build)
set(OPENSSL_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/third-party/openssl)
set(OPENSSL_INCLUDE_DIR ${OPENSSL_INSTALL_DIR}/include)
set(MSYS_SHELL ${MSYS2_ROOT}/msys2_shell.cmd)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(OPENSSL_CONFIGURE_COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c
"${OPENSSL_SOURCE_DIR}/Configure --prefix=${OPENSSL_INSTALL_DIR} --openssldir=${OPENSSL_INSTALL_DIR} --release -static no-shared mingw64")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(OPENSSL_CONFIGURE_COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c
"${OPENSSL_SOURCE_DIR}/Configure --prefix=${OPENSSL_INSTALL_DIR} --openssldir=${OPENSSL_INSTALL_DIR} --debug -static no-shared mingw64")
else()
set(OPENSSL_CONFIGURE_COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c
"${OPENSSL_SOURCE_DIR}/Configure --prefix=${OPENSSL_INSTALL_DIR} --openssldir=${OPENSSL_INSTALL_DIR} -static no-shared mingw64")
endif()
add_custom_target(OpenSSL_Configured
COMMAND ${OPENSSL_CONFIGURE_COMMAND}
WORKING_DIRECTORY ${OPENSSL_BINARY_DIR}
BYPRODUCTS ${OPENSSL_BINARY_DIR}/Makefile
DEPENDS ${OPENSSL_SOURCE_DIR}/Configure
COMMENT "OpenSSL is configuring")
add_custom_target(OpenSSL_Built
COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c "make -j${N}"
WORKING_DIRECTORY ${OPENSSL_BINARY_DIR}
BYPRODUCTS ${OPENSSL_BINARY_DIR}/libssl.a ${OPENSSL_BINARY_DIR}/libcrypto.a
DEPENDS OpenSSL_Configured
COMMENT "OpenSSL is building")
add_custom_target(OpenSSL_Installed
COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c "make install"
WORKING_DIRECTORY ${OPENSSL_BINARY_DIR}
BYPRODUCTS ${OPENSSL_INSTALL_DIR}/lib/libssl.a ${OPENSSL_INSTALL_DIR}/lib/libcrypto.a ${OPENSSL_INSTALL_DIR}/lib/libssl.dll.a ${OPENSSL_INSTALL_DIR}/lib/libcrypto.dll.a ${OPENSSL_INSTALL_DIR}/lib/libssl.dll ${OPENSSL_INSTALL_DIR}/lib/libcrypto.dll
DEPENDS OpenSSL_Built
COMMENT "OpenSSL is installing")
add_custom_target(OpenSSL DEPENDS OpenSSL_Installed COMMENT "OpenSSL is ready")
file(MAKE_DIRECTORY ${OPENSSL_INCLUDE_DIR})
add_library(OpenSSL::SSL STATIC IMPORTED GLOBAL)
set_property(TARGET OpenSSL::SSL PROPERTY IMPORTED_LOCATION ${OPENSSL_INSTALL_DIR}/lib/libssl.a)
set_property(TARGET OpenSSL::SSL PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
add_dependencies(OpenSSL::SSL OpenSSL)
add_library(OpenSSL::Crypto STATIC IMPORTED GLOBAL)
set_property(TARGET OpenSSL::Crypto PROPERTY IMPORTED_LOCATION ${OPENSSL_INSTALL_DIR}/lib/libcrypto.a)
set_property(TARGET OpenSSL::Crypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
add_dependencies(OpenSSL::Crypto OpenSSL)
add_library(ssl STATIC IMPORTED GLOBAL)
set_property(TARGET ssl PROPERTY IMPORTED_LOCATION ${OPENSSL_INSTALL_DIR}/lib/libssl.a)
set_property(TARGET ssl PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
add_dependencies(ssl OpenSSL)
add_library(crypto STATIC IMPORTED GLOBAL)
set_property(TARGET crypto PROPERTY IMPORTED_LOCATION ${OPENSSL_INSTALL_DIR}/lib/libcrypto.a)
set_property(TARGET crypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
add_dependencies(crypto OpenSSL)
set(_gRPC_SSL_LIBRARIES ssl crypto)
set(_gRPC_SSL_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
add_subdirectory(third-party/grpc ${CMAKE_CURRENT_BINARY_DIR}/third-party/grpc)
get_filename_component(hw_proto "${CMAKE_CURRENT_SOURCE_DIR}/protobufs/helloworld.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")
add_custom_command(
OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
COMMAND protobuf::protoc
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
-I "${hw_proto_path}"
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
"${hw_proto}"
DEPENDS "${hw_proto}")
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
add_library(hw_grpc_proto
${hw_grpc_srcs}
${hw_grpc_hdrs}
${hw_proto_srcs}
${hw_proto_hdrs})
target_link_libraries(hw_grpc_proto grpc++ grpc++_reflection libprotobuf)
add_executable(${CMAKE_PROJECT_NAME}_server server.cpp)
add_executable(${CMAKE_PROJECT_NAME}_client client.cpp)
target_link_libraries(${CMAKE_PROJECT_NAME}_server grpc++ grpc++_reflection libprotobuf hw_grpc_proto)
target_link_libraries(${CMAKE_PROJECT_NAME}_client grpc++ grpc++_reflection libprotobuf hw_grpc_proto)
My directory structure looks like the following:
grpc is cloned with all submodules without boringssl
openssl is cloned with all submodules
protobufs contains the .proto files that are being compiled during the build process
Contents of the .cpp files are identical to those in the helloworld example in the grpc repo
What could be the reason of my problems? Am I missing something? Thanks in advance.
EDIT3: I've actually realized a big blunder here. You can't link against the ws2_32.lib as it was built with MSVC and not MinGW. However the issue to your problem stems from here.
I feel like a donkey, but apparently newer versions of MSYS2/MinGW should be able to link against these static libraries. So the following solution is worth a try.
EDIT: After some digging in I think I found the reason why MSVC is supported and mingw is not. The reason why you're having issues is because they are most likely using #pragma to link their libraries (this is a MSVC specific feature).
Try additionaly linking to this library: ws2_32 (maybe the 64 bit version is called ws2_64) - (more precisely you want to link against winsock2 64 bit version)
EDIT2: They are all named ws2_32 and the #pragma picks the right one for you, i.e. the only thing different is the path to the correct SDK
Based on this official github issue. The GRPC team does not support MinGW for Windows builds.
They describe the same issue that you are describing and they also have a suggestion on how to fix boringssl to work i.e.
By applying this patch and
by building boringssl with the following define OPENSSL_NO_ASM.
Long story short, I would switch to MSVC on Windows if I were you - I don't see a reason why not to use it if your target platform is Windows anyway.
EDIT: However if you are still persistent on using MinGW then maybe to maybe get you closer to how to fix your issue I would maybe point out that gai_strerrorW is a function defined in ws2tcpip.h

Cannot compile curl and libgcc as a static library in MinGW/CLion (c++)

I am trying to compile curl and libgcc as a static library so I would not have to include dll files with my program. I am having problems with libcurl.dll not found when I run the executable through command prompt. Also, there is another error libgcc_s_dw2-1.dll not found.
It works when I put the dll files in the same folder as executable but that is not what I am really fond of. I have put -DCURL_STATICLIB in CMAKE_CXX_FLAGS as suggested here:
https://curl.haxx.se/docs/install.html
Here is my CMakeLists.txt
project(ham)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_C_FLAGS -m32 -DCURL_STATICLIB)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -std=c++14 -LC:/curlx86 -lcurl")
set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -static-libgcc -static-libstdc++ -lwsock32 -lws2_32 -lwinmm")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -Wl,--no-whole-archive")
project(untitled3)
add_executable(ham Ham/ham.cpp)
target_link_libraries(ham "C:/curlx86/libcurl.dll.a")
I want my program to run without including any extra dll files. What should I do ?
Edit
When I use
target_link_libraries(ham "C:/curlx86/libcurl.dll.a")
I get these errors.
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(easy.o):(.text+0x8f): undefined reference to libssh2_init'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(easy.o):(.text+0x16b): undefined reference tolibssh2_exit'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0xcd): undefined reference to nghttp2_version'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x2ca): undefined reference tonghttp2_submit_rst_stream'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x2dc): undefined reference to nghttp2_session_send'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x31d): undefined reference tonghttp2_session_set_stream_user_data'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x3c0): undefined reference to nghttp2_pack_settings_payload'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x4bd): undefined reference tonghttp2_session_resume_data'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x4fe): undefined reference to nghttp2_session_mem_recv'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x51c): undefined reference tonghttp2_strerror'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x5c3): undefined reference to nghttp2_priority_spec_init'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x614): undefined reference tonghttp2_submit_priority'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x621): undefined reference to `nghttp2_session_send'
Part 1. Setting up openssl v1.1.1b
Download the openssl v1.1.1b package (not the installer)
Download and install MSYS2
Open msys.exe (not mingw32.exe or mingw.exe)
Run this command to install perl and other packages:
pacman -S make perl msys2-devel libcrypt-devel perl-CPAN mingw-w64-i686-toolchain
Now close msys.exe and open mingw32.exe
Change directory to wherever you extracted the openssl package, in my case it's this:
cd "C:\Users\John\Downloads\openssl-1.1.1b\openssl-1.1.1b"
Before continuing any further you have to add #include <windows.h> to these files underneath the directory:
crypto\dso\dso_win32.c
crypto\init.c
Run (this compiles openssl as a static library):
./configure --prefix=c:/OpenSSLx86 --openssldir=c:/OpenSSLx86 no-threads no-idea no-shared mingw
Run make clean
Run make depend
Run make
Run make install
Part 2. Building curl with openssl v1.1.1b
Download and extract the curl package
Change directory:
cd "C:\Users\John\Downloads\curl\curl-7.65.0"
Edit lib\curl_setup.h and add the following lines to reduce the size of the compiled library by choosing only the options you will use. I will be using only the SMTP protocol, so I am disabling the others.
#define CURL_DISABLE_TFTP
#define CURL_DISABLE_FTP
#define CURL_DISABLE_LDAP
#define CURL_DISABLE_TELNET
#define CURL_DISABLE_DICT
#define CURL_DISABLE_FILE
#define CURL_DISABLE_RTSP
#define CURL_DISABLE_POP3
#define CURL_DISABLE_IMAP
//#define CURL_DISABLE_SMTP
#define CURL_DISABLE_GOPHER
#define CURL_DISABLE_SMB
Set OPENSSL_PATH = C:\OpenSSLx86 in these files:
lib\Makefile.m32
src\Makefile.m32
Run make mingw32-ssl
Now copy libcurl.a from lib to C:\curlx86 and you are good to go.
Here is my final CMakeLists.txt:
cmake_minimum_required(VERSION 3.3) #curl
project(ham)
set(CMAKE_CXX_STANDARD 14)
add_compile_definitions(
CURL_STATICLIB
WITH_SSL=STATIC
)
#CURL_DISABLE_SMTP
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -LC:/curlx86 -lcurl -LC:/OpenSSLx86/lib -lssl -LC:/OpenSSLx86/lib -lcrypto")
set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -static-libgcc -static-libstdc++ -lwsock32 -lws2_32 -lwinmm -lcrypt32")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive")
project(untitled3)
add_executable(ham Ham/ham.cpp)
target_link_libraries(ham "C:/curlx86/libcurl.a" "C:/OpenSSLx86/lib/libssl.a" "C:/OpenSSLx86/lib/libcrypto.a")

Poco library linking errors in ubuntu platform

I have downloaded the latest Poco library poco-1.7.3.tar. Configured with few properties and did make install.
Tried the following sample helloworld program.
#include <iostream>
#include <Poco/Util/Application.h>
class HelloPocoApplication : public Poco::Util::Application
{
protected:
virtual int main(const std::vector<std::string> &args)
{
std::cout << "Hello, POCO C++ Libraries!" << std::endl;
return EXIT_OK;
}
};
POCO_APP_MAIN(HelloPocoApplication);
Compiled it using
g++ -I poco-1.7.3/Util/include -I poco-1.7.3/XML/include -I poco-1.7.3/JSON/include -I poco-1.7.3/Foundation/include -L poco-1.7.3/lib/Linux/x86_64 -lPocoUtil -lPocoFoundation -lPocoXML -lPocoJSON helloworld.cpp -o prog
But it is throwing following errors
/tmp/ccFvl4ll.o: In function `main':
helloworld.cpp:(.text+0x4f): undefined reference to `Poco::Util::Application::init(int, char**)'
helloworld.cpp:(.text+0xd9): undefined reference to `Poco::Logger::log(Poco::Exception const&)'
/tmp/ccFvl4ll.o: In function `Poco::RefCountedObject::release() const':
helloworld.cpp:(.text._ZNK4Poco16RefCountedObject7releaseEv[_ZNK4Poco16RefCountedObject7releaseEv]+0x6e): undefined reference to `Poco::Bugcheck::unexpected(char const*, int)'
/tmp/ccFvl4ll.o: In function `Poco::Util::Application::logger() const':
helloworld.cpp:(.text._ZNK4Poco4Util11Application6loggerEv[_ZNK4Poco4Util11Application6loggerEv]+0x2c): undefined reference to `Poco::Bugcheck::nullPointer(char const*, char const*, int)'
/tmp/ccFvl4ll.o: In function `HelloPocoApplication::HelloPocoApplication()':
helloworld.cpp:(.text._ZN20HelloPocoApplicationC2Ev[_ZN20HelloPocoApplicationC5Ev]+0x14): undefined reference to `Poco::Util::Application::Application()'
/tmp/ccFvl4ll.o:(.gcc_except_table+0x2c): undefined reference to `typeinfo for Poco::Exception'
/tmp/ccFvl4ll.o: In function `Poco::AutoPtr<HelloPocoApplication>::operator->()':
helloworld.cpp:(.text._ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv[_ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv]+0x3a): undefined reference to `Poco::NullPointerException::NullPointerException(int)'
helloworld.cpp:(.text._ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv[_ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv]+0x3f): undefined reference to `Poco::NullPointerException::~NullPointerException()'
helloworld.cpp:(.text._ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv[_ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv]+0x44): undefined reference to `typeinfo for Poco::NullPointerException'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x20): undefined reference to `Poco::Util::Application::name() const'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x28): undefined reference to `Poco::Util::Application::initialize(Poco::Util::Application&)'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x30): undefined reference to `Poco::Util::Application::uninitialize()'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x38): undefined reference to `Poco::Util::Application::reinitialize(Poco::Util::Application&)'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x40): undefined reference to `Poco::Util::Application::defineOptions(Poco::Util::OptionSet&)'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x48): undefined reference to `Poco::Util::Application::run()'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x50): undefined reference to `Poco::Util::Application::handleOption(std::string const&, std::string const&)'
/tmp/ccFvl4ll.o: In function `HelloPocoApplication::~HelloPocoApplication()':
helloworld.cpp:(.text._ZN20HelloPocoApplicationD2Ev[_ZN20HelloPocoApplicationD5Ev]+0x1f): undefined reference to `Poco::Util::Application::~Application()'
/tmp/ccFvl4ll.o:(.rodata._ZTI20HelloPocoApplication[_ZTI20HelloPocoApplication]+0x10): undefined reference to `typeinfo for Poco::Util::Application'
collect2: error: ld returned 1 exit status
Can you please help me?
I managed to run exactly that code but using CMake rather than Makefile and it worked. you have just to change the paths according to your machine.
Here's my CMakeLists.txt :
cmake_minimum_required(VERSION 2.8.3)
project(tutocpp14)
set(Poco_DIR "/usr/local/lib/cmake/Poco/")
set(Poco_INCLUDE_DIRS "/usr/include/Poco/")
find_package(Poco REQUIRED COMPONENTS Foundation Net XML Util) # add other components here
# check c++11 / c++0x
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11 " COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "-std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "-std=c++0x")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
include_directories( ${Poco_INCLUDE_DIRS})
add_executable(publisher src/publisher.cpp)
target_link_libraries(publisher ${Poco_LIBRARIES})
Cheers,
Checking with Poco documents it seems it should work. Looking at an example here, and its makefile, I suspect the order of your library is reversed. Assuming that paths are correct (since ld didn't report on that) I would say that linking order is incorrect. See here Why.
So I suggest you change your command to
g++ -I poco-1.7.3/Util/include -I poco-1.7.3/Foundation/include -L poco-1.7.3/lib/Linux/x86_64 -lPocoFoundation -lPocoUtil helloworld.cpp -o prog
in case those examples goes out of scope somehow in future, here is the code and makefile copied.
#include <iostream>
#include <Poco/Util/Application.h>
class HelloPocoApplication : public Poco::Util::Application
{
protected:
virtual int main(const std::vector<std::string> &args)
{
std::cout << "Hello, POCO C++ Libraries!" << std::endl;
return EXIT_OK;
}
};
POCO_APP_MAIN(HelloPocoApplication);
Note: Remember to restore the tabs instead of spaces in makefile code!
POCO_LIBS=-lPocoFoundation -lPocoUtil
all: hello-poco
clean:
rm -f hello-poco
hello-poco: hello-poco.cpp
$(CXX) $(POCO_LIBS) -o hello-poco hello-poco.cpp
Update:
With all above said, I have to put a note on strangeness of your error especially Poco::Logger which is found in foundation library. So it seems that library does not get linked. I would suggest two things:
1. Check if its not corrupted. You can do that by cleaning and recompiling
2. Try to install in standard path so that you avoid linking paths
I know all these are not definitive (guess works at best) but will be useful to provide you hints of where the issue lies.
Your original library linking order was fine, but you are missing XML and JSON (Util depends on these libraries). Although, the link errors you've got indicate that you may be either missing Foundation library or mixing library versions.
$ echo $POCO_BASE
/tmp/poco
$ pwd
/tmp/poco/test
$ g++ -I $POCO_BASE/Util/include -I $POCO_BASE/Foundation/include -L $POCO_BASE/lib/Linux/x86_64 -lPocoUtil -lPocoXML -lPocoJSON -lPocoFoundation helloworld.cpp -o prog
try this command to compile.
g++ <src_file>.cpp -lPocoFoundation -lPocoJSON -lPocoUtil -lPocoJWT
-o <binary_name>

Cmake ld error while compiling

Hello i am trying to compile a program with cmake. but when ever i run make install. its gives this error. I have never worked with Cmake before so its all new to me. plz help me.
libstructure.so: undefined reference to `AptamerTree::~AptamerTree()'
libparserfastq.so: undefined reference to `ParserMain::ProcessLaneInParallel(std::string, std::string, std::string, std::string, std::string, int)'
libstructure.so: undefined reference to `AptamerTree::GetRoot()'
libparserfastq.so: undefined reference to `ParserMain::ParserMain(Parameters*, std::vector<AptamerPool*, std::allocator<AptamerPool*> >*, _win_st*)'
libparserfastq.so: undefined reference to `ParserMain::StoreStatistics()'
libparserfastq.so: undefined reference to `ParserMain::IsGZipped(std::string)'
libparserfastq.so: undefined reference to `ParserMain::StoreConsensusSizes()'
libparserfastq.so: undefined reference to `ParserMain::ProcessLaneInParallel(std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, int, int, int)'
libparserfastq.so: undefined reference to `ParserMain::~ParserMain()'
libstructure.so: undefined reference to `AptamerTree::AptamerTree()'
libstructure.so: undefined reference to `AptamerTree::InsertEdge(Node*, Node*)'
libparserfastq.so: undefined reference to `ParserMain::SetStatus(std::string, int, int)'
libparserfastq.so: undefined reference to `ParserMain::StoreQualities()'
libstructure.so: undefined reference to `AptamerTree::begin()'
libparserfastq.so: undefined reference to `ParserMain::StoreFrequencies()'
libparserfastq.so: undefined reference to `ParserMain::StoreRandomizedRegionSizes()'
libstructure.so: undefined reference to `AptamerTree::end()'
libstructure.so: undefined reference to `AptamerTree::CreateNode()'
libparserfastq.so: undefined reference to `ParserMain::LogStatistics()'
collect2: error: ld returned 1 exit status
make[2]: *** [src/htsaptamotiftest] Error 1
make[1]: *** [src/CMakeFiles/htsaptamotiftest.dir/all] Error 2
make: *** [all] Error 2
Here is the Cmakelist.txt
#this is just a basic CMakeLists.txt, for more information see the cmake manpage
MESSAGE("CONFIGURING HTSAptamotif")
# set variables required for build
get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} PATH)
SET(INSTALL_PATH ${CMAKE_INSTALL_PREFIX})
SET(EXTERNAL_INCLUDE_PATH ${INSTALL_PATH}/include/)
SET(EXTERNAL_LIB_PATH ${INSTALL_PATH}/lib/)
# use, i.e. don't skip the full RPATH for the build tree
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
# when building, don't use the install RPATH already
# (but later on when installing)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
SET(CMAKE_INSTALL_RPATH "${INSTALL_PATH}/lib")
# add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# the RPATH to be used when installing, but only if it's not a system directory
LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${INSTALL_PATH}/lib" isSystemDir)
IF("${isSystemDir}" STREQUAL "-1")
SET(CMAKE_INSTALL_RPATH "${INSTALL_PATH}/lib")
ENDIF("${isSystemDir}" STREQUAL "-1")
# we need openMP
INCLUDE(FindOpenMP)
IF(OPENMP_FOUND)
MESSAGE(" FOUND OPENMP")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
ENDIF()
#and ncurses
FIND_LIBRARY(NCURSRS_PANEL_LIBRARY NAMES panel DOC "The ncureses panel library")
INCLUDE(FindCurses)
IF(CURSES_FOUND)
MESSAGE(" FOUND CURSES")
# add panels to the library path
SET(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${NCURSRS_PANEL_LIBRARY})
ENDIF()
#add definitions, compiler switches, etc.
#-g is with debugging info. disable this for production code
#-lprofiler uses google cpu profiler. disable for production
# debugging flags
ADD_DEFINITIONS(-Wall -lmysqlcppconn -std=gnu++0x -ltcmalloc -lncurses -lpanel -O2 -g -lprofiler )
# productions flags
# ADD_DEFINITIONS(-Wall -lmysqlcppconn -std=gnu++0x -ltcmalloc -lncurses -lpanel -O2)
#OLD ADD_DEFINITIONS(-Wall -lncurses -lpanel -lmysqlcppconn -std=gnu++0x -O2 -g -ltcmalloc -lprofiler)
#ADD_DEFINITIONS(-Wno-sign-compare)
# define include and link directories
INCLUDE_DIRECTORIES(
${EXTERNAL_INCLUDE_PATH}
# /usr/local/google-perftools/1.8.2/include
)
LINK_DIRECTORIES(
${EXTERNAL_LIB_PATH}
# /usr/local/google-perftools/1.8.2/lib
)
#Define non-header libs here
MESSAGE(" Boost_LIBRARIES: ${Boost_LIBRARIES}")
MESSAGE(" Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
SET(MySQL_LIBRARIES
${EXTERNAL_LIB_PATH}libmysqlcppconn.so
)
MESSAGE(" MySQL_LIBRARIES: ${MySQL_LIBRARIES}")
SET(ViennaRNA_LIBRARIES
${EXTERNAL_LIB_PATH}libRNA.a
)
MESSAGE(" ViennaRNA_LIBRARIES: ${ViennaRNA_LIBRARIES}")
SET(GPTOOLS_LIBRARIES
${EXTERNAL_LIB_PATH}libprofiler.so
${EXTERNAL_LIB_PATH}libtcmalloc.so
)
MESSAGE(" GooglePrefTools_LIBRARIES: ${GPTOOLS_LIBRARIES}")
MESSAGE(" Curses_LIBRARIES: ${CURSES_LIBRARIES}")
#build the shared libraries
ADD_LIBRARY(htsaptamotif SHARED htsaptamotif.cpp)
ADD_LIBRARY(parameters SHARED parameters.cpp)
ADD_LIBRARY(aptamer SHARED aptamer.cpp)
ADD_LIBRARY(aptamertree SHARED aptamertree.cpp)
ADD_LIBRARY(databasesocket SHARED databasesocket.cpp)
ADD_LIBRARY(aptamerpool SHARED aptamerpool.cpp)
ADD_LIBRARY(structure SHARED structure.cpp)
ADD_LIBRARY(substructure SHARED substructure.cpp)
ADD_LIBRARY(substructureensemble SHARED substructureensemble.cpp)
# ADD_LIBRARY(aptagraph SHARED aptagraph.cpp) #templates
ADD_LIBRARY(numericalintegrator SHARED numericalintegrator.cpp)
# ADD_LIBRARY(mstprims SHARED mstprims.cpp) #templates
ADD_LIBRARY(aptamotif SHARED aptamotif.cpp)
ADD_LIBRARY(poolgenerator SHARED poolgenerator.cpp)
ADD_LIBRARY(lshcluster SHARED lshcluster.cpp)
ADD_LIBRARY(dnacompressor SHARED dnacompressor.cpp)
ADD_LIBRARY(aptamercluster SHARED aptamercluster.cpp)
ADD_LIBRARY(parsermain SHARED parsermain.cpp)
ADD_LIBRARY(parserfastq SHARED parserfastq.cpp)
ADD_LIBRARY(parserfasta SHARED parserfasta.cpp)
ADD_LIBRARY(parsertext SHARED parsertext.cpp)
#external alignment library
ADD_LIBRARY(nwaligner SHARED ssw.c)
ADD_LIBRARY(nwalignerwrapper SHARED ssw_cpp.cpp)
#for testing the shared library you probably need some test app too
ADD_EXECUTABLE(htsaptamotiftest htsaptamotiftest.cpp)
#need to link to some other libraries ? just add them here
TARGET_LINK_LIBRARIES(parameters ${Boost_LIBRARIES} ${GPTOOLS_LIBRARIES} ${CURSES_LIBRARIES})
TARGET_LINK_LIBRARIES(databasesocket ${MySQL_LIBRARIES})
TARGET_LINK_LIBRARIES(aptamer ${ViennaRNA_LIBRARIES})
TARGET_LINK_LIBRARIES(htsaptamotiftest htsaptamotif parameters databasesocket aptamerpool aptamer aptamertree aptamotif structure substructure substructureensemble numericalintegrator poolgenerator parsermain parserfastq parserfasta parsertext lshcluster dnacompressor aptamercluster nwaligner nwalignerwrapper ${GPTOOLS_LIBRARIES} ${CURSES_LIBRARIES}) #aptagraph mstprims are templates
# compile the socket program for gui comunication
ADD_EXECUTABLE(guisocket guisocket.cpp)
TARGET_LINK_LIBRARIES(guisocket ${Boost_LIBRARIES} parameters)
#add and install target here
INSTALL(TARGETS htsaptamotiftest htsaptamotif parameters databasesocket aptamerpool aptamer aptamertree structure substructure substructureensemble numericalintegrator aptamotif poolgenerator parsermain parserfastq parsertext parserfasta lshcluster dnacompressor aptamercluster nwaligner nwalignerwrapper guisocket
RUNTIME DESTINATION ${INSTALL_PATH}/bin
LIBRARY DESTINATION ${INSTALL_PATH}/lib
ARCHIVE DESTINATION ${INSTALL_PATH}/lib
)
any idea's how to solve this problem?
You need to link all yout binaries with all libraries they use. Your libstructure and libparserfastq are most likely not linked with libraries containing symbols mentioned in the errors. You need to add appropriate TARGET_LINK_LIBRARIES commands for those libraries.

FLTK 1.3 Linking errors

I update my FLTK from 1.1 to 1.3, then I can't compile my code which is worked well before update.
...
[100%] Building CXX object CMakeFiles/PROSTVIEW.dir/include/nifti/vtkznzlib.cxx.o
Linking CXX executable PROSTVIEW
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `fontopen(char const*, int, bool, int)':
fl_font.cxx:(.text+0x298): undefined reference to `FcPatternCreate'
fl_font.cxx:(.text+0x362): undefined reference to `FcPatternAddString'
fl_font.cxx:(.text+0x3e3): undefined reference to `FcPatternAddString'
fl_font.cxx:(.text+0x3f7): undefined reference to `FcPatternAddInteger'
fl_font.cxx:(.text+0x40b): undefined reference to `FcPatternAddInteger'
fl_font.cxx:(.text+0x424): undefined reference to `FcPatternAddDouble'
fl_font.cxx:(.text+0x43c): undefined reference to `FcPatternAddString'
fl_font.cxx:(.text+0x4df): undefined reference to `FcMatrixRotate'
fl_font.cxx:(.text+0x4f7): undefined reference to `FcPatternAddMatrix'
fl_font.cxx:(.text+0x516): undefined reference to `FcPatternAddBool'
fl_font.cxx:(.text+0x52c): undefined reference to `FcPatternAddBool'
fl_font.cxx:(.text+0x549): undefined reference to `XftFontMatch'
fl_font.cxx:(.text+0x5a0): undefined reference to `XftFontOpen'
fl_font.cxx:(.text+0x5b0): undefined reference to `FcPatternDestroy'
fl_font.cxx:(.text+0x5f3): undefined reference to `XftFontOpenPattern'
fl_font.cxx:(.text+0x603): undefined reference to `FcPatternDestroy'
fl_font.cxx:(.text+0x659): undefined reference to `XftFontOpenXlfd'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `utf8extents(Fl_Font_Descriptor*, char const*, int, _XGlyphInfo*)':
fl_font.cxx:(.text+0x84b): undefined reference to `XftTextExtents32'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `fl_xft_width(Fl_Font_Descriptor*, unsigned int*, int)':
fl_font.cxx:(.text+0x98a): undefined reference to `XftTextExtents32'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `fl_destroy_xft_draw(unsigned long)':
fl_font.cxx:(.text+0x102e): undefined reference to `XftDrawChange'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `Fl_Xlib_Graphics_Driver::draw(char const*, int, int, int)':
fl_font.cxx:(.text+0x10c6): undefined reference to `XftDrawCreate'
fl_font.cxx:(.text+0x10f6): undefined reference to `XftDrawChange'
fl_font.cxx:(.text+0x1140): undefined reference to `XftDrawSetClip'
fl_font.cxx:(.text+0x1202): undefined reference to `XftDrawString32'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `fl_drawUCS4(Fl_Graphics_Driver*, unsigned int const*, int, int, int)':
fl_font.cxx:(.text+0x1313): undefined reference to `XftDrawCreate'
fl_font.cxx:(.text+0x1343): undefined reference to `XftDrawChange'
fl_font.cxx:(.text+0x138d): undefined reference to `XftDrawSetClip'
fl_font.cxx:(.text+0x1433): undefined reference to `XftDrawString32'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `Fl_Xlib_Graphics_Driver::rtl_draw(char const*, int, int, int)':
fl_font.cxx:(.text+0x1480): undefined reference to `FcUtf8Len'
fl_font.cxx:(.text+0x14ff): undefined reference to `FcUtf8ToUcs4'
collect2: ld returned 1 exit status
make[2]: *** [PROSTVIEW] Error 1
make[1]: *** [CMakeFiles/PROSTVIEW.dir/all] Error 2
make: *** [all] Error 2
Here's my CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
PROJECT(PROSTVIEW)
SET(CMAKE_BUILD_TYPE DEBUG)
SET(CMAKE_CXX__FLAGS -O3 -pipe -ffast-math -Wno-deprecated)
# Chargement Package
# FLTK
FIND_PACKAGE ( FLTK )
IF ( FLTK_FOUND )
INCLUDE_DIRECTORIES( ${FLTK_INCLUDE_DIR} )
ENDIF ( FLTK_FOUND )
# ITK
FIND_PACKAGE ( ITK )
IF ( ITK_FOUND )
INCLUDE( ${USE_ITK_FILE} )
ENDIF( ITK_FOUND )
# VTK
FIND_PACKAGE ( VTK )
IF ( VTK_FOUND )
INCLUDE( ${USE_VTK_FILE} )
ENDIF( VTK_FOUND )
SET(VTK_LIBRARIES vtkCommon vtkRendering vtkWidgets vtkGraphics vtkGenericFiltering vtkzlib)
# vtkFlRenderWindowInteractor
SET(VTKFL_INCLUDE_DIR "include/vtkfl")
SET(VTKFL_CPP
${VTKFL_INCLUDE_DIR}/vtkFlRenderWindowInteractor.cxx)
# Auxiliary_Tools
SET(AT_DIR "include/Auxiliary_Tools")
SET(AT_INCLUDE_DIR ${AT_DIR}/include)
SET(AT_LIBRARY_DIR ${AT_DIR}/lib)
#Nifty
SET(NII_DIR "include/nifti")
SET(NII_SRC
${NII_DIR}/vtkNIfTIReader.cxx
${NII_DIR}/vtkNIfTIWriter.cxx
${NII_DIR}/vtkAnalyzeReader.cxx
${NII_DIR}/vtkAnalyzeWriter.cxx
${NII_DIR}/vtknifti1_io.cxx
${NII_DIR}/vtkznzlib.cxx)
#Other include
SET(PROSTVIEW_INCLUDE_DIR "include")
SET(PROSTVIEW_SRC
${PROSTVIEW_INCLUDE_DIR}/ContourTmt.cpp
${PROSTVIEW_INCLUDE_DIR}/conversion.cpp
${PROSTVIEW_INCLUDE_DIR}/TmtMatrix.cpp)
# VOLUME Widget
SET(VOLUME_WIDGET_DIR "include/GUI")
SET(VOLUME_WIDGET
${VOLUME_WIDGET_DIR}/VOLUME_Loader.cpp
${VOLUME_WIDGET_DIR}/VOLUME_Widget.cpp
${VOLUME_WIDGET_DIR}/VOLUME_Mouse_Widget.cpp
${VOLUME_WIDGET_DIR}/VOLUME_3DView.cpp
${VOLUME_WIDGET_DIR}/CONTOUR_Loader.cpp
${VOLUME_WIDGET_DIR}/ProstView.cpp)
# MODULES
SET(MODULE_DIR "Module")
# SEGMENTATION
SET(SEGMENTATION_DIR "Module/Segmentation")
SET(SEGMENTATION
${SEGMENTATION_DIR}/DDC2D.cpp
${SEGMENTATION_DIR}/DDC3D.cpp
${SEGMENTATION_DIR}/DDC3DHu.cpp
${SEGMENTATION_DIR}/DDC3DPlus.cpp
${SEGMENTATION_DIR}/DDC3DBal.cpp
${SEGMENTATION_DIR}/IMAGE_ENERGY.cpp
${SEGMENTATION_DIR}/REGULARIZATION.cpp
${SEGMENTATION_DIR}/OPTIMAL_SURFACE_DETECTION.cpp
${SEGMENTATION_DIR}/OPTIMAL_MULTIPLE_SURFACES_DETECTION.cpp
${SEGMENTATION_DIR}/RESAMPLE_From_Prostate.cpp
${SEGMENTATION_DIR}/RECTAL_WALL_US.cpp
${SEGMENTATION_DIR}/BLADDER_MRI.cpp
${SEGMENTATION_DIR}/RECTUM_MRI.cpp
${SEGMENTATION_DIR}/REGION_GROWING.tpp )
#MESH
SET(MESH_DIR "Module/Mesh")
SET(MESH
${MESH_DIR}/GENERIC_MESH.cpp
${MESH_DIR}/ELLIPSOID_MESH.cpp
${MESH_DIR}/PROSTATE_MESH.cpp
${MESH_DIR}/TUBULAR_MESH.cpp
${MESH_DIR}/OPEN_MESH.cpp
${MESH_DIR}/QUADRIC_MESH.cpp
${MESH_DIR}/ELLIPTICAL_CYLINDER_MESH.cpp
${MESH_DIR}/CONTOURS_MAN_MESH.cpp)
#STUDY
SET(STUDY_DIR "Module/Statistics")
SET(STUDY
${STUDY_DIR}/PROFILE.cpp
${STUDY_DIR}/Histogram.cpp
${STUDY_DIR}/HISTOGRAM_FROM_MESH.cpp
${STUDY_DIR}/TRAINING_SET_ALIGNMENT.cpp
${STUDY_DIR}/APPEARANCE_MODEL.cpp)
# MOMENTS
SET(MOMENTS_DIR "Module/Moments")
SET(MOMENTS
${MOMENTS_DIR}/KRAWTCHOUK_Moments.tpp
${MOMENTS_DIR}/TCHEBICHEF_Moments.tpp
${MOMENTS_DIR}/GEOMETRIC_Moments.tpp)
#VALIDATION
SET(VALIDATION_DIR "Module/Evaluation")
SET(VALIDATION
${VALIDATION_DIR}/Metrics.cpp)
# Include et Link Directories
INCLUDE_DIRECTORIES(${FLTK_INCLUDE_DIR} ${VTK_INCLUDE_DIRS} ${VTKFL_INCLUDE_DIR} ${AT_INCLUDE_DIR} ${NII_DIR} ${PROSTVIEW_INCLUDE_DIR} ${VOLUME_WIDGET_DIR} ${MODULE_DIR})
LINK_DIRECTORIES(${FLTK_LIBRARY_DIR} ${VTK_LIBRARY_DIRS} ${AT_LIBRARY_DIR} ${PROSTVIEW_INCLUDE_DIR} ${VOLUME_WIDGET_DIR} ${MODULE_DIR})
# Sources
SET(SRC main.cpp)
# Executables and "stand-alone " librairies
ADD_EXECUTABLE(PROSTVIEW ${SRC} ${PROSTVIEW_SRC} ${VTKFL_CPP} ${VOLUME_WIDGET} ${SEGMENTATION} ${MESH} ${STUDY} ${MOMENTS} ${VALIDATION} ${NII_SRC})
# Linkage
TARGET_LINK_LIBRARIES(PROSTVIEW ${FLTK_LIBRARIES} ${VTK_LIBRARIES} ${ITK_LIBRARIES})
I tried to add some libs in my CMakeLists.txt, but it's not work. Who can tell me what’s the problem?
From FLTK 1.3's README.CMake.txt:
USING CMAKE WITH FLTK
This howto assumes that you have FLTK libraries which were built using
CMake, installed. Building them with CMake generates some CMake
helper files which are installed in standard locations, making FLTK
easy to find and use.
Here is a basic CMakeLists.txt file using FLTK.
cmake_minimum_required(VERSION 2.6)
project(hello)
find_package(FLTK REQUIRED NO_MODULE) include(${FLTK_USE_FILE})
add_executable(hello WIN32 hello.cxx)
target_link_libraries(hello fltk)
The find_package command tells CMake to find the package FLTK,
REQUIRED means that it is an error if it's not found. NO_MODULE tells
it to search only for the FLTKConfig file, not using the
FindFLTK.cmake supplied with CMake, which doesn't work with this
version of FLTK.
Once the package is found we include the ${FLTK_USE_FILE} which adds
the FLTK include directories and library link information to its
knowledge base. After that your programs will be able to find FLTK
headers and when you link the fltk library, it automatically links the
libraries fltk depends on.
It seems that from FLTK 1.1 to 1.3 they changed the method needed to find the library, maybe for comparability/testing reasons?
I got hung up on the same issue, hope someone finds this useful. :)