C++ cmake 的“未定义引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16558282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:28:06  来源:igfitidea点击:

'undefined reference to' with cmake

c++cmake

提问by hal7df

So, I am trying to write an applet for school, and part of it requires using cmake. I have two different classes contained in their own files, and I use them as part of the main class. I have included their headers as such in the main project header:

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

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

The problem I have is that when I run make after I've run cmake, I get undefined reference errors for any instance in which I try to use one of the two libraries. I know that it has to do with linker errors, but since I'm new to cmake, I'm not exactly sure what the proper way to use TARGET_LINK_LIBRARIES would be.

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

EDIT

编辑

After linking/associating my libraries, I have the following:

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

CMakeLists.txt:

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

There are also too many errors to put here. Here's a few:

也有太多错误不能放在这里。这里有一些:

/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&)'

回答by hal7df

I was working with some others and I got it compiled!

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

All I really needed was to provide the name of the library and put it into target_link_librarieslike so:

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

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

My dad, being completely unfamiliar with CMake, went and dug out the link.txtwithin the CMake build structure. There, he just added the following after the -oflag:

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

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

When I saw that, I thought I might try to do that linking through CMakeLists.txt -- and it worked.

当我看到它时,我想我可能会尝试通过 CMakeLists.txt 进行链接——并且它奏效了。