Aug 3, 2018 at 11:50am UTC
Hello,
I'm having a problem linking a c library to c++ project.
In the build I'm facing the following error..
Test.cpp:25: undefined reference to `browse'
collect2: error: ld return 1 exit status
file utils.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef UTILS_H
#define UTILS_H
#ifdef __cplusplus
extern "C" {
#endif
#pragma once
// several includes..
char * browse(const char * param);
// more functions and variables....
#ifdef __cplusplus
}
#endif
#endif
file utils.c
1 2 3 4
#include "utils.h"
char *browse(const char *param){
// do stuff here
}
file Test.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "utils.h"
void Test::getTestParams() {
try {
string url = _management.getTestUrl();
char * paramBuf = browse(url.c_str());
cout << "Params: " << paramBuf << endl;
} catch (std::exception &e){
string errorMsg = "getTestParams @Test: error " + string(e.what());
_textOp.printLog(errorMsg);
_textOp.writeToLogFile(-1, errorMsg);
}
}
CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
cmake_minimum_required (VERSION 3.5)
set (proj_name launcherSpeedtest)
project (${proj_name})
#set(CMAKE_C_FLAGS "-std=c99")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -pthread" )
set (src_dir "${PROJECT_SOURCE_DIR}/src/" )
file (GLOB src_files "${src_dir}/*" )
file (GLOB pugixml_files "${src_dir}/pugixml-1.9/*.cpp" )
file (GLOB base64_files "${src_dir}/base64/*.cpp" )
find_package(Boost 1.58 COMPONENTS filesystem regex REQUIRED)
set(CURL_LIBRARY "-lcurl" )
find_package(CURL REQUIRED)
if (Boost_FOUND AND CURL_FOUND)
include_directories(${Boost_INLCUDE_DIRS} ${CURL_INCLUDE_DIR})
add_executable (${proj_name} ${src_files} ${pugixml_files} ${base64_files})
target_link_libraries(${proj_name} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
endif()
I've read several threads about similar issues but I could figure it out...
any help is welcome, thanks in advance
Last edited on Aug 3, 2018 at 11:55am UTC
Aug 3, 2018 at 12:06pm UTC
This is partially a guess, but have you tried changing it to
1 2 3 4 5
extern "C" {
#include "utils.h"
}
// ...
in your .cpp file?
Last edited on Aug 3, 2018 at 12:07pm UTC
Aug 3, 2018 at 12:14pm UTC
Yeah, I've already tried that and I ended up with the same result..
Aug 3, 2018 at 12:55pm UTC
Undefined reference means the linker can't find it. Which means one of the following:
test.c doesn't contain the function (or it's been name-mangled)
test.c isn't being compiled
The compiled test.o object isn't being linked against
The compiled test.o object is being linked in the wrong order
Start by doing the build and link yourself, at the command line, to see if it does actually work. If it does, then you know you've got a cmake problem.
Last edited on Aug 3, 2018 at 1:10pm UTC