[英]Call C function in C++ class implementation
I have to use https://github.com/google/gumbo-parser library that is written in C. 我必须使用用C编写的https://github.com/google/gumbo-parser库。
I have a HtmlParser
class which is defined in HtmlParser.h
and I implement it's methods in HtmlParser.cpp
我有一个
HtmlParser
这是在定义的类HtmlParser.h
,我实现它的方法HtmlParser.cpp
I include gumbo.h
in HtmlParser.h
and call it's functions in implemented by me getLinks(...)
function that is in HtmlParser.cpp
我在
HtmlParser.h
包含gumbo.h
, HtmlParser.h
其为由我实现的函数,该函数是HtmlParser.cpp
getLinks(...)
函数
When I try to compile it I get undefined reference to 'gumbo_parse' How can I fix it? 当我尝试对其进行编译时,会得到未定义的“ gumbo_parse”引用,该如何解决?
My makefile is 我的makefile是
cmake_minimum_required(VERSION 3.3)
project(WebCrawler)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp HtmlParser.cpp HtmlParser.h)
add_executable(WebCrawler ${SOURCE_FILES})
The undefined reference
is an error at link time. undefined reference
是链接时出错。 It means the symbol (function) that you're using and for which the definition was found when compiling the compilation unit cannot be resolved at link time to link against. 这意味着在链接时无法解析正在使用的符号(功能),并且在编译编译单元时已找到其定义的符号(功能)。
If you build in only one command, you probably just need to add a -lgumbo
to your command line, eventually with -L<path to directory containing libgumbo.so>
if it's not in the default lib path. 如果仅构建一个命令,则可能只需要在命令行中添加
-lgumbo
,如果不是默认的lib路径,则最终使用-L<path to directory containing libgumbo.so>
。 Typically: 通常:
g++ main.cc -lgumbo
or if gumbo lib and headers are in gumbo subdirectories: 或gumbo lib和标头位于gumbo子目录中:
g++ main.cc -I/usr/local/include/gumbo/ -L/usr/local/lib/gumbo/ -lgumbo
If you build in multiple command lines (first building objects, then linking them, then you need to add the -l
(and eventually -L
) options to the link command: 如果要构建多个命令行(首先构建对象,然后链接它们),则需要在链接命令中添加
-l
(最后是-L
)选项:
g++ main.cc -o main.o # This is the objects building command
g++ main.o -l gumbo # This is the linking command
Edit: With cmake
(that I now see you're using), you must tell that you're using gumbo library. 编辑:使用
cmake
(我现在看到您正在使用),您必须告诉您正在使用gumbo库。 This should be done using find_library : 这应该使用find_library完成:
find_library(gumbo)
If not supported you may need to use link_directories
to specify where to find it. 如果不支持,则可能需要使用
link_directories
指定在哪里找到它。 Then use target_link_libraries
to specify to link with this library for your target. 然后,使用
target_link_libraries
指定要与此目标库链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.