简体   繁体   中英

How to link spacy-cpp lib with CMake

I'm working on a project atm where I want to use the lib spacy-cpp . It's a header-only wrapper for the lib spaCy which is a Python lib.

Now the problem is that I'm not able to properly link the lib when using CMake but it works if I use a makefile. Here's an example how a working makefile looks like:

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)

The important part is LIBS .

Here's how I tried to add it in my 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})

This leads to the error:

====================[ 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.

How can I translate this into my CMake file? I read all the related posts on here but nothing seems to work. I appreciate all the help I could get from you.

We now found the solution to my problem, maybe in the future someone else has the same problem so I wanted to post it here. This worked for me:

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}")

Thanks for all the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM