如何使用 CMake link spacy-cpp lib

How to link spacy-cpp lib with CMake

我正在做一个项目 atm,我想在其中使用库 spacy-cpp。 它是 lib spaCy 的 header-only 包装器,它是一个 Python lib.

现在的问题是我在使用 CMake 时无法正确 link lib,但如果我使用 makefile 则它可以工作。这是一个工作生成文件的示例:

CXX = g++ -g -std=c++0x
MAIN = $(basename $(wildcard *Main.cpp))
OBJECTS = $(addsuffix .o, $(filter-out %Main %Test, $(basename $(wildcard *.cpp))))
HEADERS = $(wildcard *.h)
LIBS = -lspacy -I/usr/include/python3.8

.PRECIOUS: %.o

all: compile

compile: $(MAIN)

%Main: %Main.o $(OBJECTS)
    $(CXX) -o $@ $^ $(LIBS)

%.o: %.cpp $(HEADERS)
    $(CXX) -c $< $(LIBS)

clean:
    rm -f *.o
    rm -f $(MAIN)

重要的部分是LIBS.

以下是我尝试将其添加到我的 CMake 中的方法:

cmake_minimum_required(VERSION 3.21)
project(untitled)

set(CMAKE_CXX_STANDARD 14)

find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})

add_executable(untitled main.cpp)
target_link_libraries(untitled ${PYTHON_LIBRARIES})

这会导致错误:

====================[ Build | untitled | Debug ]================================
/home/me/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/213.7172.20/bin/cmake/linux/bin/cmake --build /home/me/desktop/dir/untitled/cmake-build-debug --target untitled
[1/2] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
FAILED: CMakeFiles/untitled.dir/main.cpp.o 
/usr/bin/c++  -I/usr/include/python3.8 -g -std=gnu++14 -MD -MT CMakeFiles/untitled.dir/main.cpp.o -MF CMakeFiles/untitled.dir/main.cpp.o.d -o CMakeFiles/untitled.dir/main.cpp.o -c /home/me/desktop/dir/untitled/main.cpp
In file included from /usr/local/include/spacy/attrs.h:12,
                 from /usr/local/include/spacy/spacy:12,
                 from /home/me/desktop/dir/untitled/main.cpp:2:
/usr/local/include/spacy/pyobjectptr.h:32:10: fatal error: pyobjectptr.cpp: File or directory not found
   32 | #include "pyobjectptr.cpp"
      |          ^~~~~~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.

如何将其转换为我的 CMake 文件? 我在这里阅读了所有相关帖子,但似乎没有任何效果。感谢您提供的所有帮助。

我们现在找到了解决我的问题的方法,也许将来会有其他人 有同样的问题所以我想在这里 post 它。 这对我有用:

project(untitled)

set(CMAKE_CXX_STANDARD 14)
find_library(SPACY_LIB spacy)
include_directories(/usr/include/python3.8)

add_executable(untitled main.cpp)
target_link_libraries(untitled PUBLIC "${SPACY_LIB}")

感谢大家的帮助。