1

I am new to C++ and have just started to use Cmake for linking the libraries to my project. I need to use a library:

https://github.com/Gnimuc/FastPD

Fortunately, I managed to build the library using Cmake (in my build there is no *.lib file at all), but I don't know how to link it to my project. I mean that I don't know how to add it to my cmakelists.txt :

(PS. I'm also using two other libraries ITK and VTK; but I can't link the above mentioned library to my project or main.cpp.)

################################################ 
cmake_minimum_required(VERSION 2.8)

project(My_project)

find_package(ITK REQUIRED)

include(${ITK_USE_FILE})

if (ITKVtkGlue_LOADED)

     find_package(VTK REQUIRED)

     include(${VTK_USE_FILE})

else()

    find_package(ItkVtkGlue REQUIRED)

    include(${ItkVtkGlue_USE_FILE})

    set(Glue ItkVtkGlue)

endif()


add_executable(My_project MACOSX_BUNDLE main.cpp)

target_link_libraries(My_project

  ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

################################################  

Thanks in advance for your helps,

drescherjm
  • 10,365
  • 5
  • 44
  • 64
ir0098
  • 127
  • 1
  • 13
  • `(in my build there is no *.lib file at all)` - Project referred by you **definitely creates the library**. Library's extension depends on operatin system and [CMake generator](https://cmake.org/cmake/help/v3.7/manual/cmake-generators.7.html) which is used when build the library. – Tsyvarev Apr 11 '17 at 21:59
  • Tsyvarev, thanks for your comment. I've used both cmake 2.0.8 and 3.0.2 and win64; but there is no *.lib. Could you please build it? – ir0098 Apr 11 '17 at 22:27
  • I have looked into the project referenced - it doesn't exports any symbol. In that case Visual Studio doesn't generate `.lib` file. You may fill the bugreport for the project's developer about that. – Tsyvarev Apr 12 '17 at 07:26
  • Possible duplicate of [How do I get CMake to create a dll and its matching lib file?](http://stackoverflow.com/questions/7614286/how-do-i-get-cmake-to-create-a-dll-and-its-matching-lib-file) – Tsyvarev Apr 12 '17 at 07:27
  • Tsyyvaref, thanks guy. You mean I can't use this library anymore? – ir0098 Apr 12 '17 at 13:20
  • Yes, you cannot use given library without modifications. – Tsyvarev Apr 12 '17 at 13:42

2 Answers2

0

Assuming you installed the library and its header files, you can then search for the header files using find_path, and add the found path to the include directories. Then you can search for the library by using find_library, and add the library with the target_link_libraries command.

Gert Wollny
  • 529
  • 2
  • 8
  • Gret, thanks for the comment. Just a question, when I build this library, there is no *.lib file. I mean that after the build process there are only *.h , *.cpp, and *.dll files; and there is no *.lib file. How should I link such library? – ir0098 Apr 11 '17 at 22:18
  • You might check [this question](https://stackoverflow.com/questions/7614286/how-do-i-get-cmake-to-create-a-dll-and-its-matching-lib-file). Specifically, the FastPD project doesn't seem to explicitly export the functions and classes. On Windows one needs to decorate the functions and classes with *dllexport* when compiling them (and *dllimport* when using them in another project). An alternative for you might be to create a static library (i.e. replace the `SHARED` keyword in *CMakeLists.txt* with the `STATIC` keyword in `add_libraray`. – Gert Wollny Apr 12 '17 at 10:10
  • Again thanks Gert, In fact, I didn't get your point about the dllexport and decorating the functions. Could you please explain a little more what should I do with the functions of this library? But I changed the CMakeLists.txt as you advised, shared to static and build the library. Now, a *.lib file is created but Cmake can not link my project to that. Please have a lokk at this message: http://hpics.li/864f3cd – ir0098 Apr 12 '17 at 13:14
  • Regarding dllexport/import you can read up [here](https://msdn.microsoft.com/en-us/library/aa271769%28v=vs.60%29.aspx). With respect to your error I assume that you did use `find_path` and then used the result in `target_link_library`, but you must use the result of `find_library` (it would be helpful to update the question to reflect the changed CMakeLists.txt). – Gert Wollny Apr 13 '17 at 12:18
  • Hi Gert, again thanks for your help. I put my final CmakeLists.txt. Perhaps you could read it and improve it ... – ir0098 Apr 15 '17 at 16:52
  • My suggestion would be to not create four helper libraries for FASTPD, but just one, and instead of creating lists for Blitz++ you could use the include path and libraries directly (since both list only contain this one entry). otherwise the CMakeLists.txt looks okay to me. – Gert Wollny Apr 24 '17 at 09:43
0

I tried both shared and static types for creation of the library. In the case of shared, no *.lib file was created, and in the case of static, my project couldn't link to the library. Therefore, in my project's CmakeLists.txt, I decided to add all the *.cpp and headers as libraries and then link them together (as I didn't know the dependency among them!!!), and finally, I linked them to my project. Perhaps it does not make sense but it works; hope it helps you:

CmakeLists.txt
##############################################

cmake_minimum_required(VERSION 2.6)

set(PROJ_NAME PROJECT46)
PROJECT(${PROJ_NAME})

# Prevent compilation in-source
if( ${CMAKE_BINARY_DIR} STREQUAL ${PROJECT_SOURCE_DIR} )
  Message( " " )
  Message( FATAL_ERROR "Source and build  directories are the same.
 Create an empty build directory,
 change into it and re-invoke cmake")
endif()

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 "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support.     
Please use a different C++ compiler.")
endif()

##############################################
## External libraries
##############################################
list( APPEND CMAKE_MODULE_PATH
${PROJECT_SOURCE_DIR}/cmake
)

# Blitz
find_package( Blitz++ REQUIRED )
list( APPEND PROJ_INCLUDE_DIRS
${Blitz++_INCLUDE_DIR}
)
list( APPEND
PROJ_LIB
${Blitz++_LIBRARIES}
)










# ITK and VTK
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
else()
find_package(ItkVtkGlue REQUIRED)
include(${ItkVtkGlue_USE_FILE})
set(Glue ItkVtkGlue)
endif()
##############################################





# FASTPD
add_library(FASTPD Fast_PD.cpp Fast_PD.h common.h block.h)
add_library(GRAPH graph.h graph.cpp)
add_library(linked LinkedBlockList.h LinkedBlockList.cpp)
add_library(MAXFLOW maxflow.cpp)





include_directories(${PROJ_INCLUDE_DIRS})

add_executable(${PROJ_NAME} main.cpp)
target_link_libraries(linked MAXFLOW)
target_link_libraries(GRAPH linked)
target_link_libraries(FASTPD GRAPH)
target_link_libraries(${PROJ_NAME} FASTPD)

target_link_libraries(${PROJ_NAME}
    ${PROJ_LIB} ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES}
)
ir0098
  • 127
  • 1
  • 13