简体   繁体   中英

Call C function in C++ class implementation

I have to use https://github.com/google/gumbo-parser library that is written in C.

I have a HtmlParser class which is defined in HtmlParser.h and I implement it's methods in 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

When I try to compile it I get undefined reference to 'gumbo_parse' How can I fix it?

My makefile is

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. 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. Typically:

g++ main.cc -lgumbo

or if gumbo lib and headers are in gumbo subdirectories:

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:

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. This should be done using find_library :

find_library(gumbo)

If not supported you may need to use link_directories to specify where to find it. Then use target_link_libraries to specify to link with this library for your target.

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