-1

In order to use cmph, a perfect minimal hashing library, in my project organised using CMake, I installed cmph library in a ubuntu machine and tested it using a single c file called main.c.

If I try to compile this file using gcc 5.3.0 using the following command:

gcc main.c

I will get the following output

/tmp/ccSOH5ob.o: In function `main':
testperfect.c:(.text+0x63): undefined reference to   `cmph_io_vector_adapter'
testperfect.c:(.text+0x73): undefined reference to `cmph_config_new'
testperfect.c:(.text+0x88): undefined reference to `cmph_config_set_algo'
testperfect.c:(.text+0x9b): undefined reference to `cmph_config_set_mphf_fd'
testperfect.c:(.text+0xa7): undefined reference to `cmph_new'
testperfect.c:(.text+0xb7): undefined reference to `cmph_config_destroy'
testperfect.c:(.text+0xca): undefined reference to `cmph_dump'
testperfect.c:(.text+0xd6): undefined reference to `cmph_destroy'
testperfect.c:(.text+0xe2): undefined reference to `cmph_load'
testperfect.c:(.text+0x118): undefined reference to `cmph_search'
testperfect.c:(.text+0x153): undefined reference to `cmph_destroy'
testperfect.c:(.text+0x15f): undefined reference to  `cmph_io_vector_adapter_destroy'

But if I run this command:

gcc main.c $(pkg-config --libs cmph) -o main

It will be compiled and run normally.

Now I need to add a similar piece of code in my project and the CMakeList.txt is written like this:

set(PROJECT_EXECUTABLE ${PROJECT_NAME}) 

# Compiling flags. 
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR 
   CMAKE_CXX_COMPILER_ID MATCHES "Clang") 
  set(CMAKE_CXX_FLAGS_RELEASE        "-O2") 
  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g") 
  set(CMAKE_CXX_FLAGS_DEBUG          "-g") 
  set(CMAKE_CXX_FLAGS_MINSIZEREL     "-Os") 
  endif() 
# Inject project config. 
configure_file( 
  ${PROJECT_INCLUDE_DIR}/phsim/config.hpp.in 
  ${PROJECT_INCLUDE_DIR}/phsim/config.hpp 
  ) 

include(FindPkgConfig) 
find_package(PkgConfig REQUIRED) 
pkg_search_module(CMPH REQUIRED cmph) 
target_link_libraries(Phsim ${CMPH_LIBRARIES}) 
target_include_directories(Phsim PUBLIC ${CMPH_INCLUDE_DIRS}) 
target_compile_options(Phsim PUBLIC ${CMPH_CFLAGS_OTHER}) 

# Compile executable. 
file(GLOB SOURCES ${PROJECT_SRC_DIR}/*.cpp) 
add_executable(${PROJECT_EXECUTABLE} ${SOURCES}) 

set_target_properties(${PROJECT_EXECUTABLE} PROPERTIES 
  VERSION ${PHSIM_VERSION_LITER} 
  ) 

And then I try to run cmake . and make, but only get the error message:

CMake Error at src/CMakeLists.txt:20 (target_link_libraries):
  Cannot specify link libraries for target "Phsim" which is not built by this
  project.

But I won't get target executable file unless I compile the project. If I try to compile my project without those commands related to library linking, the compiler will give similar link errors provided in the beginning of my question.

I have checked the following questions:

Undefined reference to cmph functions even after installing cpmh library

And I tried instructions provided by these sites:

https://cmake.org/Wiki/CMake:How_To_Find_Libraries

https://cmake.org/cmake/help/v3.6/module/FindPkgConfig.html

Many thanks in advance.

Community
  • 1
  • 1
AI_
  • 1
  • 1
  • 1
  • What's the value of `CMPH_LIBRARIES`? – usr1234567 Oct 08 '16 at 11:28
  • @usr1234567 It is /usr/share/lib and I have .a and .so files matching the pattern libcmph*.* – AI_ Oct 08 '16 at 12:44
  • Phism isn't declared anywhere. Did you miss an according `add_executable`? It should be defined before you try linking stuff to it. Your automatic add_executable code looks weird, I wouldn't use such a thing. It's not CMake-style. – usr1234567 Oct 08 '16 at 13:06
  • @usr1234567 Hello, I tried your suggestion by moving those commands(include find_package pkg_search and so on) after the definition of add_executable in the CMakeLists.txt under folder Phsim/src. However, it still reports the error " cannot specify link libraries for target Phsim which is not built by this project". – AI_ Oct 08 '16 at 13:36
  • You don't have an executable Phsim, right? You cannot link to the project. Please try a minimal example (one cpp, a couple of lines of CMake code). If that does not work, post it here. If it works, try to extend it step by step until you struggle. If you cannot figure out that problem, ask again. – usr1234567 Oct 08 '16 at 16:02

1 Answers1

0

Finally Solved.

cmake_minimum_required(VERSION 3.0.2)
project(TestGamma)

set(GAMMATEST_VERSION_MAJOR 1)
set(GAMMATEST_VERSION_MINOR 0)
set(CMPH_INCLUDE_DIR /usr/local/lib)

include(FindPkgConfig)
configure_file(
    "${PROJECT_SOURCE_DIR}/TestGammaConfig.h.in"
    "${PROJECT_BINARY_DIR}/TestGammaConfig.h"
)

include_directories(${PROJECT_BINARY_DIR})

set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)

add_executable(testgamma ${SOURCE_FILES})
pkg_check_modules(CMPH REQUIRED cmph)
include_directories(${CMPH_INDLUDE_DIR})
link_directories(${CMPH_INCLUDE_DIR})
target_link_libraries(testgamma cmph ${CMPH_INCLUDE_DIR})

Make sure to include pkgconfig at first and add link operations after calling "add_executable"

@usr1234567 Thank you for your attention.

AI_
  • 1
  • 1
  • 1
  • Glad it works for you. Usually you let me answer the question, as I helped you with this. Dropping code isn't an answer, you should also explain what's the actual solution. BTW, this is such a basic mistake that it not worth keeping it at SO, at least not in the current form. I vote to close this question. – usr1234567 Oct 12 '16 at 05:31