2012-12-19 40 views
2

我正在嘗試第一次使用CMake,並且幾乎沒有成功。我的CMakeLists.txt是:爲什麼CMake生成的Makefile不能找到外部庫?

# Sets the version of CMake that is required. 
cmake_minimum_required(VERSION 2.8.10) 

# Name of the project. 
project(QTCODERLIB) 

# Adds common directories to the build. 
include_directories(/usr/local/include) 
link_directories(/usr/local/lib) 

# Check for header files that we depend on. 
include(CheckIncludeFiles) 
check_include_files(libavcodec/avcodec.h HAVE_AVCODEC_H) 

# Adds all of the source files. 
file(GLOB SOURCE RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cxx) 

# Builds a library named `qtcoder` from source. 
add_library(qtcoder SHARED ${SOURCE}) 

# Links the library against third-party dependencies. 
target_link_libraries(qtcoder avcodec) 

我test.cxx文件是:

#include "config.h" 
#include <libavcodec/avcodec.h> 

void f(void) { 
    avcodec_register_all(); 
} 

cmake的。 & &使VERBOSE = 1的結果在此:

/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -H/Users/phowes/Personal/QTCoder/QTCoderLib -B/Users/phowes/Personal/QTCoder/QTCoderLib --check-build-system CMakeFiles/Makefile.cmake 0 
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_progress_start /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles/progress.marks 
make -f CMakeFiles/Makefile2 all 
make -f CMakeFiles/qtcoder.dir/build.make CMakeFiles/qtcoder.dir/depend 
cd /Users/phowes/Personal/QTCoder/QTCoderLib && /usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_depends "Unix Makefiles" /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles/qtcoder.dir/DependInfo.cmake --color= 
make -f CMakeFiles/qtcoder.dir/build.make CMakeFiles/qtcoder.dir/build 
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_progress_report /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles 1 
[100%] Building CXX object CMakeFiles/qtcoder.dir/test.cxx.o 
/usr/bin/c++ -Dqtcoder_EXPORTS -fPIC -I/usr/local/include -o CMakeFiles/qtcoder.dir/test.cxx.o -c /Users/phowes/Personal/QTCoder/QTCoderLib/test.cxx 
Linking CXX shared library libqtcoder.dylib 
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_link_script CMakeFiles/qtcoder.dir/link.txt --verbose=1 
/usr/bin/c++ -dynamiclib -Wl,-headerpad_max_install_names -o libqtcoder.dylib -install_name /Users/phowes/Personal/QTCoder/QTCoderLib/libqtcoder.dylib CMakeFiles/qtcoder.dir/test.cxx.o -L/usr/local/lib -lavcodec 
Undefined symbols for architecture x86_64: 
    "avcodec_register_all()", referenced from: 
     f() in test.cxx.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 
make[2]: *** [libqtcoder.dylib] Error 1 
make[1]: *** [CMakeFiles/qtcoder.dir/all] Error 2 
make: *** [all] Error 2 

兩個對象文件和庫被編譯爲64位:

$ lipo -info CMakeFiles/qtcoder.dir/test.cxx.o 
Non-fat file: CMakeFiles/qtcoder.dir/test.cxx.o is architecture: x86_64 

$ lipo -info /usr/local/lib/libavcodec.dylib 
Non-fat file: /usr/local/lib/libavcodec.dylib is architecture: x86_64 

庫中的功能存在:

$ nm /usr/local/lib/libavcodec.dylib | grep avcodec_register_all 
0000000000033c01 T _avcodec_register_all 

我在這裏做錯了什麼?

+2

'ld:找不到架構x86_64的符號(也許你只有32位的庫)? –

+0

正如Daniel指出的那樣,錯誤表明您正在嘗試將32位庫鏈接到64位應用程序。如果您已經在lib64下安裝了64位庫,那麼Tom Kerr討論的查找模塊可能會有所幫助。如果你想迫使你的應用程序構建爲32位,相關的談話是在這裏:http://www.cmake.org/pipermail/cmake/2011-May/044602.html –

+0

@RyanMaloney我不認爲這是案件。 'lipo -info'報告'test.cxx.o'和'/ usr/local/lib/libavcodec.dylib'爲'architecture:x86_64'。我會用這個更新這個問題。 – Paul

回答

1

通常你會使用find_package來做這些繁重的任務。 CMake默認附帶很多模塊,但有時您必須下載一個用於較少使用的項目。

以下是關於how to find libraries的一些文檔。

以下是他們設想的例子。

cmake_minimum_required(VERSION 2.8) 
project(helloworld) 
add_executable(helloworld hello.c) 
find_package (BZip2) 
if (BZIP2_FOUND) 
    include_directories(${BZIP_INCLUDE_DIRS}) 
    target_link_libraries (helloworld ${BZIP2_LIBRARIES}) 
endif (BZIP2_FOUND) 

從簡單的網頁搜索,我不清楚CMake提供了一個AVCodec模塊。我發現了幾個在線:

https://github.com/arjanhouben/SDL_ffmpeg/blob/master/Findavcodec.cmake http://whispercast.org/trac/browser/trunk/cmake/FindLibAvCodec.cmake

我看到使用這個庫大多數人都感興趣的FFmpeg中,這可能是你所感興趣的東西了。

https://github.com/zinnschlag/openmw/blob/master/cmake/FindFFMPEG.cmake

如果你需要使用你下載一個,這裏有how to use custom modoules一些指令。

+0

太棒了!感謝指針。我將不得不多玩一點'find_package'。我已經閱讀了一些關於它的信息,但沒有完全理解我所看到的。 – Paul