Pavel Ryzhov
Pavel Ryzhov

Reputation: 5162

CMake link a library (.a/.so)

I've got following project file structure :

libs/
  └──FreeImage/
        ├── FreeImage.dll
        ├── FreeImage.h
        ├── FreeImage.lib
        ├── license-fi.txt
        ├── license-gplv2.txt
        └── license-gplv3.txt
main.cpp
CMakeLists.txt

My CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.4)
project(untitled)

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

set(FREEIMAGE_LIBRARY_AND_HEADER_DIRECTORY libs/FreeImage)
find_path(FREEIMAGE_LIBRARY_AND_HEADER_DIRECTORY FreeImage.h)
find_library(FREEIMAGE_LIBRARY_AND_HEADER_DIRECTORY freeimage)
include_directories(${FREEIMAGE_LIBRARY_AND_HEADER_DIRECTORY})

set(SOURCE_FILES main.cpp)
add_executable(main ${SOURCE_FILES})

target_link_libraries(main freeimage)

I followed this to write my CMakeLists.txt, but it doesn't work for me.

The output:

/home/username/main_ssd/soft/linux/portable/clion-140.569.17/bin/cmake/bin/cmake --build /home/username/.clion10/system/cmake/generated/70336599/70336599/Debug --target main -- -j 4
-- Configuring done
-- Generating done
-- Build files have been written to: /home/username/.clion10/system/cmake/generated/70336599/70336599/Debug
Scanning dependencies of target main
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX executable main
/usr/bin/ld: cannot find -lfreeimage
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:85: recipe for target 'main' failed
make[3]: *** [main] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/main.dir/all' failed
make[2]: *** [CMakeFiles/main.dir/all] Error 2
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/main.dir/rule' failed
make[1]: *** [CMakeFiles/main.dir/rule] Error 2
Makefile:160: recipe for target 'main' failed
make: *** [main] Error 2

Upvotes: 5

Views: 8108

Answers (3)

LaurentTrk
LaurentTrk

Reputation: 54

Peter points you to the right direction :

target_link_libraries(main freeimage)

should be replaced by :

target_link_libraries(main ${freeimage})

where ${freeimage} is the variable containing the value of the path of the library you want to link to, retrieved with find_library.

Hope it helps.

Upvotes: 0

Pavel Ryzhov
Pavel Ryzhov

Reputation: 5162

I've downloaded new version of FreeImage library (from here) and compiled it following these instructions:

Compiling FreeImagePlus
-----------------------
FreeImagePlus is a C++ wrapper for FreeImage. 
To compile FreeImage as a C++ library, follow these steps : 
1) Enter the FreeImage directory
2) Build the distribution : 
make -f Makefile.fip
make -f Makefile.fip install
3) Clean all files produced during the build process
make -f Makefile.fip clean

So I've got the following file structure of the library:

libs/FreeImage/
├── FreeImage.h
├── FreeImagePlus.h
├── libfreeimageplus-3.16.0.so
├── libfreeimageplus.a
├── license-fi.txt
├── license-gplv2.txt
└── license-gplv3.txt

And then I modified my CMakeLists.txt like this:

cmake_minimum_required(VERSION 2.8.4)
project(untitled)

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

find_path(SIFTGPU_INCLUDE_DIR FreeImage.h)
find_library(SIFTGPU_LIBRARY libfreeimageplus)
include_directories(${SIFTGPU_INCLUDE_DIR})

add_executable(main ${SOURCE_FILES})

target_link_libraries(main freeimageplus)

Upvotes: 2

Peter Petrik
Peter Petrik

Reputation: 10195

set(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY libs/FreeImage)

Variable FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY is set to libs/FreeImage

find_path(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY FreeImage.h)

Variable FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY is set to directory containing the named file FreeImage.h. No hints to the commands are present, because of missing optional arguments PATHS or HINTS

find_library(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY freeimage) Variable FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY is set to full path to the library FreeImage (if found). No hints to the commands are present, because of missing optional arguments PATHS or HINTS

include_directories(${FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY})

Variable FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY contains full path to library FreeImage (if found).

target_link_libraries(main freeimage)

No target with name freeimage exists in the CMakeLists.txt and hence it is interpreted that you want to link -lfreeimage

Question: Why do you try to use a "dll" on Linux?

Upvotes: 3

Related Questions