CMake links library (.a / .so)

I have the 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 one to write mine CMakeLists.txt

, but it doesn't work for me.

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

      

+3


source to share


3 answers


I downloaded the new version of the FreeImage library (from here ) and compiled it according to 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 have the following library file structure:



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 changed 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)

      

+1


source


set(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY libs/FreeImage)

Variable is FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY

set tolibs/FreeImage

find_path(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY FreeImage.h)

The variable is FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY

set to the directory containing the named file FreeImage.h

. No command hints, due to lack of additional PATHS or HINTS arguments

find_library(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY freeimage)

The variable is FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY

set to the full path to the library FreeImage

(if found). No command hints, due to lack of additional PATHS or HINTS arguments



include_directories(${FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY})

The variable FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY

contains the full path to the library FreeImage

(if found).

target_link_libraries(main freeimage)

There is no named target in CMakeLists.txt file FreeImage

and therefore interpreted as what you want to link-lfreeimage

Question: Why are you trying to use "dll" in Linux?

+2


source


Peter points you in the right direction:

target_link_libraries(main freeimage)

      

should be replaced with:

target_link_libraries(main ${freeimage})

      

where ${freeimage}

is a variable containing the value of the path of the library you want to bind to with find_library

.

Hope it helps.

0


source







All Articles