gpt4 book ai didi

c++ - std::vector compiler error ld: symbol(s) not found for architecture x86_64

转载 作者:行者123 更新时间:2023-12-02 10:59:36 25 4
gpt4 key购买 nike

我正在尝试在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

最佳答案

您没有针对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的惯用法)

关于c++ - std::vector compiler error ld: symbol(s) not found for architecture x86_64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47224895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com