2009-11-25 210 views
6

我想用CMake編譯使用C庫GStreamer的C++應用程序。CMake鏈接問題

我的main.cpp文件看起來像這樣:

extern "C" { 
#include <gst/gst.h> 
#include <glib.h> 
} 


int main(int argc, char* argv[]) 
{ 
    GMainLoop *loop; 

    GstElement *pipeline, *source, *demuxer, *decoder, *conv, *sink; 
    GstBus *bus; 

    /* Initialisation */ 
    gst_init (&argc, &argv); 
    return 0; 
} 

這工作:

g++ -Wall $(pkg-config --cflags --libs gstreamer-0.10) main.cpp -o MPEG4GStreamer 

如何以使其與CMake的?我的CMakeLists.txt文件看起來像這樣:

cmake_minimum_required (VERSION 2.6) 
project (MPEG4GStreamer) 
add_executable (MPEG4GStreamer main.cpp) 
include(${CMAKE_ROOT}/Modules/FindPkgConfig.cmake) 

# Set CMAKE_C_FLAGS variable with info from pkg-util 
execute_process(COMMAND pkg-config --cflags gstreamer-0.10 
       OUTPUT_VARIABLE CMAKE_C_FLAGS) 
string(REPLACE "\n" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) 
message("CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}") 

# Set CMAKE_LINKER_FLAGS variable with info from pkg-util 
execute_process(COMMAND pkg-config --libs gstreamer-0.10 
       OUTPUT_VARIABLE CMAKE_LINKER_FLAGS) 
string(REPLACE "\n" "" CMAKE_LINKER_FLAGS ${CMAKE_LINKER_FLAGS}) 
message("CMAKE_LINKER_FLAGS: ${CMAKE_LINKER_FLAGS}") 

set_target_properties(MPEG4GStreamer 
         PROPERTIES COMPILE_FLAGS ${CMAKE_C_FLAGS} 
           LINKER_FLAGS ${CMAKE_LINKER_FLAGS}) 

輸出給一個鏈接錯誤:

~ $ cmake . 
CMAKE_C_FLAGS: -D_REENTRANT -I/usr/include/libxml2 -I/opt/local/include/gstreamer-0.10 -I/opt/local/include/glib-2.0 -I/opt/local/lib/glib-2.0/include -I/opt/local/include 
CMAKE_LINKER_FLAGS: -L/opt/local/lib -lgstreamer-0.10 -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lxml2 -lpthread -lz -lm -lglib-2.0 -lintl -liconv 
-- Configuring done 
-- Generating done 
-- Build files have been written to: /Users/francis/ 
~ $ make 
Linking CXX executable MPEG4GStreamer 
Undefined symbols: 
    "_gst_init", referenced from: 
     _main in main.cpp.o 
ld: symbol(s) not found 
collect2: ld returned 1 exit status 
make[2]: *** [MPEG4GStreamer] Error 1 
make[1]: *** [CMakeFiles/MPEG4GStreamer.dir/all] Error 2 
make: *** [all] Error 2 
~ $ 

回答

5

衛生署,只是需要用CMAKE_EXE_LINKER_FLAGS更換CMAKE_LINKER_FLAGS。

+0

你應該關閉這個問題然後 – lorenzog 2009-11-25 13:21:32

2

您應該找到庫gstreamer-0.10,並使用target_link_libraries將其鏈接到目標。你需要一個FindGStream.cmake模塊,這裏是一個我發現谷歌搜索:

http://gitorious.org/phonon/import/blobs/88743646f086352c5b41544ad6b0b48d2379df62/cmake/FindGStreamer.cmake

有了這樣的模塊,你會想是這樣的:

包括(FindGStreamer.cmake) include_directories($ {} GSTREAMER_INCLUDE_DIR) add_definitions($ {} GSTREAMER_DEFINITIONS) target_link_libraries(MPEG4GStreamer $ {} GSTREAMER_LIBRARIES)

4

真正的問題是你輸入了錯誤的p roperty的名字,這是LINK_FLAGS,不LINKER_FLAGS。您的解決方案只是一種解決方法。