我正在尝试使用CMake编译使用C库GStreamer的C ++应用程序。
我的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
~ $
答案 0 :(得分:5)
Doh,只需要用CMAKE_EXE_LINKER_FLAGS替换CMAKE_LINKER_FLAGS。
答案 1 :(得分:4)
真正的问题是您错误输入了属性名称,它是 LINK_FLAGS ,而不是 LINK ER _FLAGS 即可。您的解决方案只是一种解决方法。
答案 2 :(得分:2)
您应该找到库gstreamer-0.10,并使用target_link_libraries将其链接到您的目标。你需要一个FindGStream.cmake模块,这是我在google搜索中找到的模块:
使用这样的模块,你会想要这样的东西:
包括(FindGStreamer.cmake) include_directories($ {GSTREAMER_INCLUDE_DIR}) add_definitions($ {} GSTREAMER_DEFINITIONS) target_link_libraries(MPEG4GStreamer $ {GSTREAMER_LIBRARIES})