std :: vector编译器错误ld:找不到架构x86_64的符号

时间:2017-11-10 14:22:56

标签: c++ compiler-errors clion linkage

我正在尝试在Clion中使用libnormaliz库。但是,它通过CMake /Users/soner/Downloads/normaliz-3.4.0/source/libnormaliz从其路径include_directories(/Users/soner/Downloads/normaliz-3.4.0/source/libnormaliz)直接链接 。我已经下载了released version。但运行时会出现错误,

使用Clang规范,

Undefined symbols for architecture x86_64:
  "libnormaliz::Cone<int>::Cone(libnormaliz::Type::InputType, std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > > const&)", referenced from:
      _main in main.cpp.o
  "libnormaliz::Cone<int>::~Cone()", referenced from:
      _main in main.cpp.o
  "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
  "std::logic_error::logic_error(char const*)", referenced from:
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
  "std::length_error::~length_error()", referenced from:
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
  "std::terminate()", referenced from:
      ___clang_call_terminate in main.cpp.o
  "typeinfo for std::length_error", referenced from:
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
  "vtable for std::length_error", referenced from:
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "operator delete(void*)", referenced from:
      std::__1::__vector_base<int, std::__1::allocator<int> >::~__vector_base() in main.cpp.o
      std::__1::__vector_base<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::~__vector_base() in main.cpp.o
  "operator new(unsigned long)", referenced from:
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
  "___cxa_allocate_exception", referenced from:
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
  "___cxa_begin_catch", referenced from:
      ___clang_call_terminate in main.cpp.o
  "___cxa_free_exception", referenced from:
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
  "___cxa_throw", referenced from:
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
  "___gxx_personality_v0", referenced from:
      _main in main.cpp.o
      std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in main.cpp.o
      std::__1::vector<int, std::__1::allocator<int> >::vector(std::__1::vector<int, std::__1::allocator<int> > const&) in main.cpp.o
      Dwarf Exception Unwind Info (__eh_frame) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

没有Clang规范,

Undefined symbols for architecture x86_64:
  "libnormaliz::Cone<int>::Cone(libnormaliz::Type::InputType, std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > > const&)", referenced from:
      _main in main.cpp.o
  "libnormaliz::Cone<int>::~Cone()", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64

代码,

#include <libnormaliz/libnormaliz.h>
#include <libnormaliz/cone.h>
#include <vector>

int main() {
    std::vector<std::vector <int> > data { {1,1,1}, {2,2,2} };

    libnormaliz::Type::InputType type = libnormaliz::Type::cone;
    /* runs well up to here */
    libnormaliz::Cone<int> MyCone = libnormaliz::Cone<int>(type, data);

    return 0;
}

的CMakeLists.txt,

cmake_minimum_required(VERSION 3.8)
project(untitled2)

set(CMAKE_CXX_STANDARD 14)

set(LIB_NORMALIZ_PATH /Users/soner/Downloads/normaliz-3.4.0/source/libnormaliz)

find_library(
        LIB_NORMALIZ
        libnormaliz
        PATHS
        ${LIB_NORMALIZ_PATH}
)

if (NOT FOUND_LIB_NORMALIZ)
    message(SEND_ERROR "Unable to find libnormaliz")
endif()

set(SOURCE_FILES main.cpp)
add_executable(untitled2 ${SOURCE_FILES})
target_link_libraries(untitled2 ${LIB_NORMALIZ})
target_include_directories(untitled2 PUBLIC ${LIB_NORMALIZ_PATH})

它的输出,

CMake Error at CMakeLists.txt:16 (message):
  Unable to find libnormaliz


CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LIB_NORMALIZ
    linked by target "untitled2" in directory /Users/soner/CLionProjects/untitled2

enter image description here来自reference manual

路径, enter image description here

1 个答案:

答案 0 :(得分:0)

您未与libnormaliz

相关联

您需要配置cmake以链接库。

第一步是告诉cmake如何查找您使用find_library

的库
find_library(
    LIB_NORMALIZ
    libnormaliz
    PATHS
        /Users/soner/Downloads/normaliz-3.4.0/source/libnormaliz
    )

您可以验证cmake是否能够找到它:

if (NOT FOUND_LIB_NORMALIZ)
    message(FATAL_ERROR "Unable to find libnormaliz")
endif()

cmake找到该库后,您需要使用target_link_libraries

将其链接到目标
target_link_libraries(untitled2 ${LIB_NORMALIZ})

您的CMakeLists.txt应如下所示:

cmake_minimum_required(VERSION 3.8)
project(untitled2)

set(CMAKE_CXX_STANDARD 14)

set(LIB_NORMALIZ_PATH /Users/soner/Downloads/normaliz-3.4.0/source/libnormaliz)

find_library(
    LIB_NORMALIZ
    libnormaliz
    PATHS
        ${LIB_NORMALIZ_PATH}
    )

if (NOT FOUND_LIB_NORMALIZ)
    message(SEND_ERROR "Unable to find libnormaliz")
endif()

set(SOURCE_FILES main.cpp)
add_executable(untitled2 ${SOURCE_FILES})
target_link_libraries(untitled2 ${LIB_NORMALIZ})
target_include_directories(untitled2 PUBLIC ${LIB_NORMALIZ_PATH})

(注意我已将include_directories更改为target_include_directories,因为这是现代cmake的惯用语。