相关疑难解决方法(0)

什么是未定义的引用/未解析的外部符号错误,我该如何解决?

什么是未定义的参考/未解决的外部符号错误?什么是常见原因以及如何修复/预防它们?

随意编辑/添加您自己的.

c++ c++-faq linker-errors unresolved-external undefined-reference

1418
推荐指数
32
解决办法
52万
查看次数

如何正确地将库与cmake链接?

我无法使用我正在使用的其他库来正确链接到我的项目中.

我正在使用CLion,它使用cmake来构建它的项目.我试图将几个库与OpenGL结合使用来构建一些对象.我最初在Visual Studio中构建它,因为我无法弄清楚如何让cmake与Clion一起工作.但是,现在代码全部工作(无论如何都在Visual Studio中),我希望能够使用CLion,因为这是我首选的IDE.

我还是cmake的新手,我不明白我的错误CMakeLists.txt.这是我有的:

cmake_minimum_required(VERSION 3.3)
project(texture_mapping)
find_package(OpenGL REQUIRED)
link_directories(${OPENGL_gl_LIBRARY})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp camera.h display.h display.cpp mesh.cpp mesh.h obj_loader.cpp obj_loader.h shader.cpp shader.h stb_image.c stb_image.h texture.cpp texture.h transform.h)

link_directories(texture_mapping ${PROJECT_SOURCE_DIR}/lib)

add_executable(texture_mapping ${SOURCE_FILES})

target_include_directories(texture_mapping PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(texture_mapping SDL2 SDL2main SDL2test glew32 glew32s ${OPENGL_gl_LIBRARY})
Run Code Online (Sandbox Code Playgroud)

我调整它直到它没有在CLion中给我任何更多的错误,但我的代码仍然无法识别头文件.

这是我的项目的结构:
项目结构

所以,我放了我需要的所有库,但它似乎没有在代码中识别它们.Clion在项目中识别它们(它们没有出现错误的红色),但是当它构建时(当我尝试在CLion中运行它时),我得到这些错误:

CMakeFiles\texture_mapping.dir/objects.a(mesh.cpp.obj): In function `ZN4MeshD2Ev':
...texture-mapping/mesh.cpp:30: undefined reference to `_imp____glewDeleteVertexArrays'
CMakeFiles\texture_mapping.dir/objects.a(mesh.cpp.obj): In function `ZN4Mesh8InitMeshERK12IndexedModel':
...texture-mapping/mesh.cpp:36: undefined reference to `_imp____glewGenVertexArrays'
...texture-mapping/mesh.cpp:37: undefined reference to `_imp____glewBindVertexArray'
...texture-mapping/mesh.cpp:39: undefined reference to `_imp____glewGenBuffers'
...texture-mapping/mesh.cpp:40: undefined reference …
Run Code Online (Sandbox Code Playgroud)

c++ opengl cmake undefined-reference

29
推荐指数
1
解决办法
5万
查看次数

未定义的引用

当我编译链表的代码时,我得到了一堆未定义的引用错误.代码如下.我一直在编写这两个语句:

g++ test.cpp 
Run Code Online (Sandbox Code Playgroud)

以及

g++ LinearNode.h LinearNode.cpp LinkedList.h LinkedList.cpp test.cpp  
Run Code Online (Sandbox Code Playgroud)

我真的不明白为什么我会收到这些错误,因为我对C++中的类很生疏.我真的可以使用一些帮助.

LinearNode.h:

#ifndef LINEARNODE_H
#define LINEARNODE_H
#include<iostream>

using namespace std;

class LinearNode
{
    public:
        //Constructor for the LinearNode class that takes no arguments 
        LinearNode();
        //Constructor for the LinearNode class that takes the element as an argument
        LinearNode(int el);
        //returns the next node in the set.
        LinearNode* getNext();
        //returns the previous node in the set
        LinearNode* getPrevious();
        //sets the next element in the set
        void setNext(LinearNode* node);
        //sets the previous element …
Run Code Online (Sandbox Code Playgroud)

c++ undefined-reference

23
推荐指数
3
解决办法
18万
查看次数

如何使用CMake将静态库链接到可执行文件

在CMake中,我们使用TARGET_LINK_LIBRARIES()将共享库链接到库/可执行文件.

For example:
TARGET_LINK_LIBRARIES(ExecutableName xxx)
   where ExecutableName - is the name of executable
         xxx - is the library name.
Run Code Online (Sandbox Code Playgroud)

据我所知,CMake在LINK_DIRECTORIES()宏中提到的路径中搜索"libxxx.so".但是如果我有一个名为"libxxx.a"的第三方库,那么如何使用CMake将库链接到可执行文件.

提前谢谢你的帮助!

cmake

7
推荐指数
2
解决办法
3万
查看次数