Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake compile issue #643

Closed
yanboyang713 opened this issue Feb 20, 2023 · 5 comments
Closed

cmake compile issue #643

yanboyang713 opened this issue Feb 20, 2023 · 5 comments

Comments

@yanboyang713
Copy link

Hello All,

Good day to you.

Currently, I am try to using cmake download and build the libqxx.
But, it is show libns3.37-database-default.so: undefined reference to `pqxx::internal::check_pqxx_version_7_6()
anyone know the reason why? Thanks for your help.

cmake:

include(FetchContent)

cmake_minimum_required (VERSION 3.10)
project(example VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

FetchContent_Declare(
  libpqxx
  GIT_REPOSITORY https://github.com/jtv/libpqxx.git
  #GIT_TAG 3d97c80bcde96fb70a21c1ae1cf92ad934818210
  GIT_TAG 90768b07f7feb55a9bd70cfaacb5543bfd074022
)
set(PQXX_LIBRARIES pqxx)

FetchContent_MakeAvailable(libpqxx)

set(source_files
    model/database.cc
)

set(header_files
    model/database.h
)
build_lib(
  #add_link_options(-lpqxx -lpq)

  LIBNAME database
  SOURCE_FILES ${source_files}
  HEADER_FILES ${header_files}
  LIBRARIES_TO_LINK ${PQXX_LIBRARIES}
  #LIBRARIES_TO_LINK pqxx pq
)

ERROR MSG:

/usr/bin/ld: /home/yanboyang713/projects/test/ns3-learning/ns-allinone-3.37/ns-3.37/build/lib/libns3.37-database-default.so: undefined reference to `pqxx::internal::check_pqxx_version_7_6()'
collect2: error: ld returned 1 exit status
make[3]: *** [scratch/CMakeFiles/scratch_anomalyPrediction_anomalyPrediction.dir/build.make:116: /home/yanboyang713/projects/test/ns3-learning/ns-allinone-3.37/ns-3.37/build/scratch/anomalyPrediction/ns3.37-anomalyPrediction-default] Error 1
make[2]: *** [CMakeFiles/Makefile2:5248: scratch/CMakeFiles/scratch_anomalyPrediction_anomalyPrediction.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:5254: scratch/CMakeFiles/scratch_anomalyPrediction_anomalyPrediction.dir/rule] Error 2
make: *** [Makefile:2101: scratch_anomalyPrediction_anomalyPrediction] Error 2
@tt4g
Copy link
Contributor

tt4g commented Feb 20, 2023

Commit SHA 90768b0 is included in version 7.7.3.
As far as the symbol name pqxx::internal::check_pqxx_version_7_6() is concerned, libns3.37-database-default.so expects libpqxx version 7.6.
The header file referenced when building the ns library is wrong.

@jtv
Copy link
Owner

jtv commented Feb 21, 2023

Exactly. It looks like libns was built with libpqxx 7.6, but the installed libpqxx is a different version.

This is a general problem with rich ABIs like you get in C++, and so I generally recommend using libpqxx as a static library rather than as a shared library.

@yanboyang713
Copy link
Author

yanboyang713 commented Feb 21, 2023 via email

@tt4g
Copy link
Contributor

tt4g commented Feb 21, 2023

libpqxx is a static library unless the BUILD_SHARED_LIBS variable is defined with a value that evaluates to truthy: https://cmake.org/cmake/help/v3.26/variable/BUILD_SHARED_LIBS.html

add_library(pqxx ${CXX_SOURCES})

Set BUILD_SHARED_LIBS to FALSE once before FetchContent_MakeAvailable() because FetchContent_MakeAvailable() calls FetchContent_Populate() and add_subdirectory().
See: https://cmake.org/cmake/help/v3.26/module/FetchContent.html

+set(BUILD_SHARED_LIBS_TMP ${BUILD_SHARED_LIBS})
+set(BUILD_SHARED_LIBS FALSE)
 FetchContent_MakeAvailable(libpqxx)
+set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_TMP})

And since there is no build_lib function in CMake functions, it is not clear to us what it is doing.
When linking libraries in CMake, we usually use target_link_libraries().
If you use the other way, CMake will not propagate library information that must be linked when using the pqxx target, and macros that must be defined at build time.

+add_library(${your_library_name} ${source_files ...})
+target_link_libraries(${your_library_name} PRIVATE pqxx)
-build_lib(
-  #add_link_options(-lpqxx -lpq)
-
-  LIBNAME database
-  SOURCE_FILES ${source_files}
-  HEADER_FILES ${header_files}
-  LIBRARIES_TO_LINK ${PQXX_LIBRARIES}
-  #LIBRARIES_TO_LINK pqxx pq
-)

@jtv
Copy link
Owner

jtv commented Mar 28, 2023

Looks like we're done with this ticket. Thanks once again @tt4g for jumping in so quickly.

@jtv jtv closed this as completed Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants