CMake链接问题

发布于 2024-08-12 01:17:06 字数 2241 浏览 5 评论 0原文

我正在尝试使用 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

How to I make it with 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
~ $

I am trying to use CMake to compile a C++ application that uses the C library GStreamer.

My main.cpp file looks like this:

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;
}

This works:

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

How to I make it with CMake? My CMakeLists.txt file looks like this:

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

Output give a linker error:

~ $ 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
~ $

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

起风了 2024-08-19 01:17:06

哦,只需要将 CMAKE_LINKER_FLAGS 替换为 CMAKE_EXE_LINKER_FLAGS 即可。

Doh, just needed to replace CMAKE_LINKER_FLAGS with CMAKE_EXE_LINKER_FLAGS.

相对绾红妆 2024-08-19 01:17:06

真正的问题是您输错了属性名称,它是LINK_FLAGS,而不是LINKER_FLAGS。您的解决方案只是一个解决方法。

The real problem is you mistyped the property name, it's LINK_FLAGS, not LINKER_FLAGS. Your solution is just a workaround.

纵情客 2024-08-19 01:17:06

您应该找到库 gstreamer-0.10,并使用 target_link_libraries 将其链接到您的目标。你需要一个 FindGStream.cmake 模块,这是我通过谷歌搜索找到的一个:

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

使用这样的模块,你会想要这样的东西:

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

You should find the library gstreamer-0.10, and use target_link_libraries to link it to your target. You need a FindGStream.cmake module, here is one I found with a google search:

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

With a module like that, you would want something like this:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文