[CMake] CMake and BZip2

Rolf Eike Beer eike at sf-mail.de
Thu Feb 11 18:44:44 EST 2016


Am Donnerstag, 11. Februar 2016, 23:56:54 schrieb Kristian:
> Hey,
> 
> I have a small C++ file, which uses some functions of the library
> bzip2. On a Debian derivate, I have installed needed packages with
> 
>  * sudo apt-get install bzip2 libzip2 libbz2-dev
> 
> Here is my CMakeLists.txt:
> 
> =============================
> 
> project(bzip2Example)
> cmake_minimum_required(VERSION 2.8)
> 
> find_package(BZip2)
> include_directories(${BZIP2_INCLUDE_DIR})
> target_link_libraries(${BZIP2_LIBRARIES})
> 
> set(BZIP2_EX_SRC main.cpp)
> 
> add_executable(${PROJECT_NAME} ${BZIP2_EX_SRC})
> 
> =============================
> 
> Then I type in: cmake . && make
> 
> This is the output of the make command:
> =============================
> main.cpp: In function ‘int main(int, char**)’:
> main.cpp:8:21: warning: deprecated conversion from string constant to
> ‘char*’ [-Wwrite-strings]
>   char *sourceFile = "test.txt";
>                      ^
> main.cpp:9:21: warning: deprecated conversion from string constant to
> ‘char*’ [-Wwrite-strings]
>   char *targetFile = "test.bz2";
>                      ^
> Linking CXX executable bzip2Ex
> CMakeFiles/bzip2Ex.dir/main.cpp.o: In Funktion `main':
> main.cpp:(.text+0x15c): Nicht definierter Verweis auf `BZ2_bzWriteOpen'
> main.cpp:(.text+0x193): Nicht definierter Verweis auf `BZ2_bzWrite'
> main.cpp:(.text+0x1db): Nicht definierter Verweis auf `BZ2_bzWriteClose'
> main.cpp:(.text+0x267): Nicht definierter Verweis auf `BZ2_bzWriteClose'
> collect2: error: ld returned 1 exit status
> make[2]: *** [bzip2Ex] Fehler 1
> make[1]: *** [CMakeFiles/bzip2Ex.dir/all] Fehler 2
> make: *** [all] Fehler 2
> 
> =============================
> 
> Why there is the error?

You aren't using target_link_libraries right. You need to do:

 target_link_libraries(${PROJECT_NAME} ${BZIP2_LIBRARIES})

HTH

Eike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160212/e20e0139/attachment.sig>


More information about the CMake mailing list