DuckDB C++ 链接错误:未定义的引用

问题描述 投票:0回答:1

我尝试在我的项目中使用 C++ 使用 DuckDB,但遇到了链接错误。

我将 duckdb 文件路径存储在名为 DUCKDB_DIR 的环境变量中,并尝试使用 CMake 为一个简单的程序生成 make 文件,该程序仅尝试调用

Connection::Query()

下面是我的CMakeFile:

# Set the minimum required CMake version
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_COMPILER g++)
project(column_order)

# DuckDB
set(DUCKDB_DIR $ENV{DUCKDB_DIR})
if(NOT DUCKDB_DIR)
    message(FATAL_ERROR "DUCKDB_DIR not set. Please set the DUCKDB_DIR environment variable.")
else()
    message(STATUS "DUCKDB_DIR set to ${DUCKDB_DIR}")
endif()

# Check if the libduckdb library is found
find_library(DUCKDB_LIB duckdb PATHS ${DUCKDB_DIR})
if(NOT DUCKDB_LIB)
    message(FATAL_ERROR "libduckdb not found. Please check the DUCKDB_DIR environment variable.")
else()
    message(STATUS "libduckdb found at ${DUCKDB_LIB}")
endif()

# Add the executable
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)

# Specify include and link directories
target_include_directories(${PROJECT_NAME} PRIVATE ${DUCKDB_DIR})

# Link the DuckDB library
target_link_libraries(${PROJECT_NAME} PRIVATE ${DUCKDB_DIR}/libduckdb.so)

然后我使用 cmake 成功生成了 Makefile,并且消息显示

libduckdb.so
已找到。但是当我尝试使用 make 命令时,在链接阶段发生错误(目标文件
main.o
已生成)并且消息是

/usr/bin/ld: CMakeFiles/column_order.dir/src/main.cpp.o: in function `main':
main.cpp:(.text+0x1c2): undefined reference to `duckdb::Connection::Query(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: CMakeFiles/column_order.dir/src/main.cpp.o: in function `duckdb::unique_ptr<duckdb::MaterializedQueryResult, std::default_delete<duckdb::MaterializedQueryResult>, true>::AssertNotNull(bool)':
...<some other similar errors>
collect2: error: ld returned 1 exit status

任何人都可以了解正在发生的事情吗?

我还尝试直接使用 Makefile 生成代码,将

main.cpp
duckdb.hpp
libduckdb.so
放在同一文件夹中并使用以下 Makefile:

# Compiler settings - Can change to clang++ if preferred
CXX=g++
CXXFLAGS=-std=c++11 -Wall

# Since the header and so files are in the same directory as the Makefile,
# I just used -I. for includes
INCLUDES=-I.

LDFLAGS=-L.

# Libraries to link against
LDLIBS=libduckdb.so

# Name of the output program
TARGET=main

# Source files
SOURCES=main.cpp
OBJECTS=$(SOURCES:.cpp=.o)


# Rule to create object files
%.o: %.cpp
    $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

# Rule to link the program
$(TARGET): $(OBJECTS)
    $(CXX) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $@

# Clean the build
clean:
    rm -f $(OBJECTS) $(TARGET)

# Build all targets
all: clean $(TARGET)

仍然出现同样的错误。

ldd libduckdb.so
的输出:

linux-vdso.so.1 (0x00007ffe2edd9000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f95389cf000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f95389ac000)
    libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f95387ca000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f953867b000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f9538660000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f953846c000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f953b2a1000)

cmake -S ../
的输出(我在构建目录中运行
cmake
):

 -- The C compiler identification is GNU 9.4.0
   -- The CXX compiler identification is GNU 9.4.0
   -- Check for working C compiler: /usr/bin/cc
   -- Check for working C compiler: /usr/bin/cc -- works
   -- Detecting C compiler ABI info
   -- Detecting C compiler ABI info - done
   -- Detecting C compile features
   -- Detecting C compile features - done
   -- Check for working CXX compiler: /usr/bin/g++
   -- Check for working CXX compiler: /usr/bin/g++ -- works
   -- Detecting CXX compiler ABI info
   -- Detecting CXX compiler ABI info - done
   -- Detecting CXX compile features
   -- Detecting CXX compile features - done
   -- DUCKDB_DIR set to /home/Caius/projects/column_order/third_party/duckdb
   -- libduckdb found at /home/Caius/projects/column_order/third_party/duckdb/libduckdb.so
   -- Configuring done
   -- Generating done
   -- Build files have been written to: /home/Caius/projects/column_order/build

ls $DUCKDB_DIR 的输出:

duckdb.h  duckdb.hpp  libduckdb-linux-amd64.zip  libduckdb.so  libduckdb_static.a
c++ cmake linker-errors duckdb
1个回答
0
投票

根据 Botje 和 n.m.couldbeanAI 的建议,这是由 CXX11 ABI 不匹配引起的。我尝试在 CMakeLists.txt 中添加

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
以使用旧版本,它解决了链接错误。

© www.soinside.com 2019 - 2024. All rights reserved.