Paul Meyer
Paul Meyer

Reputation: 87

PostgreSQL & CMakeLists - Undefined symbols [...] _my_sock_read in libpq.a(fe-secure-openssl.o)

Hi stackoverflow community,

I currently face the issue that I want to link postgresql's library and I must have accomplished this to a certain extend because I commented my target_link_library(cpp-service ${postgres_LIBRARY_FILES}) and got other errors regarding the linking and library of postgressql.

I currently have PostgreSQL 9.6.3

Following error occurs:

[ 33%] Linking CXX executable cpp-service
Undefined symbols for architecture x86_64:
  "_ASN1_STRING_data", referenced from:
      _verify_peer_name_matches_certificate_name in libpq.a(fe-secure-openssl.o)
  "_ASN1_STRING_length", referenced from:
      _verify_peer_name_matches_certificate_name in libpq.a(fe-secure-openssl.o)
  "_BIO_clear_flags", referenced from:
      _my_sock_read in libpq.a(fe-secure-openssl.o)
      _my_sock_write in libpq.a(fe-secure-openssl.o)
  "_BIO_int_ctrl", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
  "_BIO_new", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
  "_BIO_s_socket", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
  "_BIO_set_flags", referenced from:
      _my_sock_read in libpq.a(fe-secure-openssl.o)
      _my_sock_write in libpq.a(fe-secure-openssl.o)
  "_CRYPTO_get_id_callback", referenced from:
      _pgtls_close in libpq.a(fe-secure-openssl.o)
      _pgtls_init in libpq.a(fe-secure-openssl.o)
  "_CRYPTO_get_locking_callback", referenced from:
      _pgtls_close in libpq.a(fe-secure-openssl.o)
      _pgtls_init in libpq.a(fe-secure-openssl.o)
  "_CRYPTO_num_locks", referenced from:
      _pgtls_init in libpq.a(fe-secure-openssl.o)
  "_CRYPTO_set_id_callback", referenced from:
      _pgtls_close in libpq.a(fe-secure-openssl.o)
      _pgtls_init in libpq.a(fe-secure-openssl.o)
  "_CRYPTO_set_locking_callback", referenced from:
      _pgtls_close in libpq.a(fe-secure-openssl.o)
      _pgtls_init in libpq.a(fe-secure-openssl.o)
  "_ENGINE_by_id", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
  "_ENGINE_finish", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
      _pgtls_close in libpq.a(fe-secure-openssl.o)
  "_ENGINE_free", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
      _pgtls_close in libpq.a(fe-secure-openssl.o)
  "_ENGINE_init", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
  "_ENGINE_load_private_key", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
  "_ERR_clear_error", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
      _pgtls_read in libpq.a(fe-secure-openssl.o)
      _pgtls_write in libpq.a(fe-secure-openssl.o)
  "_ERR_get_error", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
      _pgtls_read in libpq.a(fe-secure-openssl.o)
      _pgtls_write in libpq.a(fe-secure-openssl.o)
  "_ERR_put_error", referenced from:
      _pgtls_open_client in libpq.a(fe-secure-openssl.o)
  "_ERR_reason_error_string", referenced from:

[...] The errors message continues like that and ends with classic make errors

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [cpp-service] Error 1
make[2]: *** [CMakeFiles/cpp-service.dir/all] Error 2
make[1]: *** [CMakeFiles/cpp-service.dir/rule] Error 2
make: *** [cpp-service] Error 2

My CMakeLists.txt has includes the library, alongside with pqxx (just a snippet from my bigger CMakeLists.txt file)

#libpqxx
set(libpqxx_SOURCE "/usr/local/opt/libpqxx" )

if ( libpqxx_SOURCE )
    set( libpqxx_FOUND TRUE )
    set( libpqxx_INCLUDE "${libpqxx_SOURCE}/include" )
    set( libpqxx_LIBRARY "${libpqxx_SOURCE}/lib" )

    message( STATUS "${Green}Found libpqxx include at: ${libpqxx_INCLUDE}${Reset}" )
else ( )
    message( FATAL_ERROR "${Red}Failed to locate libpqxx dependency.${Reset}" )
endif ( )


#postgres
set(postgres_SOURCE "/usr/local/opt/postgres" )

if ( postgres_SOURCE )
    set( postgres_FOUND TRUE )
    set( postgres_INCLUDE "${postgres_SOURCE}/include" )
    set( postgres_LIBRARY "${postgres_SOURCE}/lib" )

    message( STATUS "${Green}Found Postgres include at: ${postgres_INCLUDE}${Reset}" )
else ( )
    message( FATAL_ERROR "${Red}Failed to locate Postgres dependency.${Reset}" )
endif ( )

#libpqxx
file(GLOB_RECURSE libpqxx_LIBRARY_FILES
        "${libpqxx_LIBRARY}/*.a"
        "${libpqxx_LIBRARY}/*.dylib")
#postgres
file(GLOB_RECURSE postgres_LIBRARY_FILES
        "${postgres_LIBRARY}/*.a"
        "${postgres_LIBRARY}/*.dylib")

add_executable(cpp-service ${SOURCE})

target_link_libraries(cpp-service ${libpqxx_LIBRARY_FILES})
target_link_libraries(cpp-service ${postgres_LIBRARY_FILES})

I tried to include OpenSSL and crypto, thinking that may be missing but it did not change a thing.

OS: OS X 10.12.5 IDE: CLion

Looking for a hint for cause and solution for this problem.

Upvotes: 0

Views: 732

Answers (1)

Paul Meyer
Paul Meyer

Reputation: 87

I found the error. Basically, postgres lib directory contains both *.dylib and *.a files. I try to link both of them as well as I had no trouble with doing this using boost or libpqxx.

Before (ERROR)

file(GLOB_RECURSE postgres_LIBRARY_FILES "${postgres_LIBRARY}/*.a" "${postgres_LIBRARY}/*.dylib")

After

file(GLOB_RECURSE postgres_LIBRARY_FILES "${postgres_LIBRARY}/*.dylib")

Upvotes: 1

Related Questions