【问题标题】:'undefined reference to' with cmake'未定义的引用'与cmake
【发布时间】:2013-05-15 06:30:57
【问题描述】:

所以,我正在尝试为学校编写一个小程序,其中一部分需要使用 cmake。我有两个不同的类包含在它们自己的文件中,我将它们用作主类的一部分。我已将它们的标题包含在主项目标题中:

#include /path/to/header/1.h
#include /path/to/header/2.h

我遇到的问题是,当我在运行 cmake 后运行 make 时,对于尝试使用这两个库之一的任何实例,我都会收到未定义的引用错误。我知道这与链接器错误有关,但由于我是 cmake 新手,我不确定使用 TARGET_LINK_LIBRARIES 的正确方法是什么。

编辑

链接/关联我的库后,我有以下内容:

CMakeLists.txt:

# A name for the project
project(plasma-engine-gdrive)

# Find the required libaries
find_package(KDE4 REQUIRED)
include(KDE4Defaults)

add_definitions (${QT_DEFINITIONS} ${KDE4_DEFINITIONS}, -std=c++0x)
include_directories(
   ${CMAKE_SOURCE_DIR}
   ${CMAKE_BINARY_DIR}
   ${KDE4_INCLUDES}
   ./include
   ./lib
   )

set (GOOGLE_LIBS include/atom_helper.h include/util/string_utils.h include/client/doc_list_service.h include/client/service.h)
set (GOOGLE_SRCS include/atom_helper.cc include/util/string_utils.cc include/client/doc_list_service.cc include/client/service.cc)

# We add our source code here
set(gdrive_engine_SRCS gdriveengine.cpp)

add_library (DataWrapper include/DataWrapper.cpp include/DataWrapper.h)
add_library (GData ${GOOGLE_SRCS} ${GOOGLE_LIBS})

# Now make sure all files get to the right place
kde4_add_plugin(plasma_engine_gdrive ${gdrive_engine_SRCS})
target_link_libraries(plasma_engine_gdrive
                      GData
                      DataWrapper
                      ${KDE4_KDECORE_LIBS}
                      ${KDE4_PLASMA_LIBS})

install(TARGETS plasma_engine_gdrive
        DESTINATION ${PLUGIN_INSTALL_DIR})

install(FILES plasma-engine-gdrive.desktop
        DESTINATION ${SERVICES_INSTALL_DIR})

这里也有太多的错误。这里有一些:

/usr/include/c++/4.6/bits/stl_map.h:467: undefined reference to `Glib::ustring::ustring()'
lib/libGData.a(atom_helper.o): In function `pair<Glib::ustring, Glib::ustring, void>':
/usr/include/c++/4.6/bits/stl_pair.h:132: undefined reference to `Glib::ustring::ustring(Glib::ustring const&)'
/usr/include/c++/4.6/bits/stl_pair.h:132: undefined reference to `Glib::ustring::ustring(Glib::ustring const&)'
lib/libGData.a(atom_helper.o): In function `pair<Glib::ustring, Glib::ustring>':
/usr/include/c++/4.6/bits/stl_pair.h:137: undefined reference to `Glib::ustring::ustring(Glib::ustring const&)'
/usr/include/c++/4.6/bits/stl_pair.h:137: undefined reference to `Glib::ustring::ustring(Glib::ustring const&)'

【问题讨论】:

  • 这就是您实际编写#include 指令的方式吗?不像#include "/path/to/header/1.h"(注意路径周围的引号)?
  • 当我听到小程序时,我听到了 java
  • 实际例子,也许
  • @JoachimPileborg 是的,这就是我写#include 的方式。放在这里的时候忘记写引号了。
  • 这些是第一个打印错误吗? Glib::ustring 引用从何而来?在使用 Qt 和 KDE 的项目中看到对 Glib 的引用很奇怪。另请注意,您在 CMakeLists 中经常引用目标 plasma_engine_gdrive,但从您发布的内容来看,这实际上不是一个有效的目标。您不应该在 CMake 中使用项目名称作为目标标识符。

标签: c++ cmake


【解决方案1】:

我和其他一些人一起工作,我把它编译了!

我真正需要的只是提供库的名称并将其放入target_link_libraries,如下所示:

target_link_libraries(plasma_engine_gdrive
                      DataWrapper
                      GData
                      xml++-2.6
                      curl
                      glibmm-2.4
                      ${KDE4_KDECORE_LIBS}
                      ${KDE4_PLASMA_LIBS})

我父亲对 CMake 完全不熟悉,因此去挖掘了 CMake 构建结构中的 link.txt。在那里,他只是在-o 标志之后添加了以下内容:

-lxml++-2.6
-lcurl
-lglibmm-2.4

当我看到这一点时,我想我可能会尝试通过 CMakeLists.txt 进行链接——它成功了。

【讨论】:

    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 2015-12-06
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多