English 中文(简体)
CMake linking problem
原标题:

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 "
" "" 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 "
" "" 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
~ $
最佳回答

Doh, just needed to replace CMAKE_LINKER_FLAGS with CMAKE_EXE_LINKER_FLAGS.

问题回答

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

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





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签