CMake 'undefined reference' error with CERN-ROOT - c++

I'm writing a small addition to CERN ROOT and now can't get rid of 'undefined reference' errors on every function from ROOT that I use (and my classes too). Can't find a way to fix this, so looking for help here.
My OS is Linux Mint 19. I use g++ as my c++ compiler, however, I tried clang++ and there was no difference.
I downloaded sources of ROOT from their github and built it now on my computer. Output of root --version is this:
ROOT Version: 6.19/01
Built for linuxx8664gcc on Sep 29 2019, 14:25:35
From heads/master#v6-19-01-1167-gbec9f2d1f7
This is the structure of my project:
ParentFolder
--CMakeLists.txt
--General
----include/...
----source/...
----CMakeLists.txt
----LinkDef.h
--Test
----include/...
----source/...
----CMakeLists.txt
Important lines from top level CMakeLists.txt are:
find_package(ROOT REQUIRED)
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
include(${ROOT_USE_FILE})
add_subdirectory("./General")
add_subdirectory("./Test")
From Test/CmakeLists.txt:
add_executable(Test source/test_main.cpp)
target_include_directories(Test PUBLIC "./include")
target_link_libraries(Test PUBLIC General GTest::GTest GTest::Main ${ROOT_LIBRARIES})
From General/CmakeLists.txt:
file(GLOB_RECURSE SOURCES . source/*.cpp)
ROOT_GENERATE_DICTIONARY(GeoManagerExtractor LINKDEF LinkDef.h)
add_library(General SHARED ${SOURCES} GeoManagerExtractor.cxx)
target_link_libraries(General ${ROOT_LIBRARIES})
target_include_directories(General PUBLIC "./include")
Here are first few lines produced by CMake (while using clang++)
CMakeFiles/Test.dir/source/test_main.cpp.o: In function `main':
test_main.cpp:(.text+0x37): undefined reference to `TGeoManager::Import(char const*, char const*, char const*)'
test_main.cpp:(.text+0x3f): undefined reference to `gGeoManager'
test_main.cpp:(.text+0x8c): undefined reference to `gGeoManager'
CMakeFiles/Test.dir/source/test_main.cpp.o: In function `TGeoManagerExporter::~TGeoManagerExporter()':
test_main.cpp:(.text._ZN19TGeoManagerExporterD2Ev[_ZN19TGeoManagerExporterD2Ev]+0xa): undefined reference to `vtable for TGeoManagerExporter'
../General/libGeneral.so: undefined reference to `ROOT::GenerateInitInstance(TGeoManagerExporter const*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here are a few lines from test_main.cpp:
TGeoManager::Import("../root_tests/geofile_full.root");
if (gGeoManager == nullptr) {
std::cerr << "Can't find TGeoManager in file" << std::endl;
return 0;
}
UPD: Forgot to mention, that in order to create my CMakeLists.txt files I used these two sources:
https://root.cern.ch/how/integrate-root-my-project-cmake
https://cliutils.gitlab.io/modern-cmake/chapters/packages/ROOT.html

I have managed to compile my project.
As it turned out, .so file that contains TGeoManager implementation was not included in ROOT_LIBRARIES(thanks, Kamil Cuk for bringing my attention to this variable) by the script provided by ROOT developers. It was hard to find as this class was working properly in Cling(c++ interpreter included with ROOT) and this class is almost the only one from ROOT that I use in the project, so I assumed, that the whole ROOT library was not linking properly. After I found this issue, I fixed my CMake files like this (this is from General/CmakeLists.txt):
file(GLOB_RECURSE SOURCES2 . $ENV{ROOTSYS}/lib/*.so)
add_library(General SHARED ${SOURCES} ${SOURCES2} GeoManagerExtractor.cxx)
This eliminates all of the "undefined reference to 'gGeoManager'" and alike, but "undefined reference to `vtable for TGeoManagerExporter'" (TGeoManagerExporter is my own class) stays. However, I figured out that this error is caused by a reflection feature, so as a temporary solution I just don't activate it. I'll post a comment here if I solve this problem.

Related

Running multiple versions of OpenCV on the same computer

My computer is running Ubuntu-16.04-LTS and OpenCV-2.4.13 is already installed on it. However, I would like to use the functionalities of newer versions of OpenCV, such as OpenCV-3.2.0 without removing the older version.
So far I have downloaded OpenCV-3.2.0 and compiled and installed it. I am uing CMake to compile OpenCV, so I changed my CMakeLists.txt file to:
cmake_minimum_required (VERSION 3.2)
project(io)
find_package(OpenCV REQUIRED)
include_directories("/home/ubuntu/opencv-3.2.0/include") # directory of OpenCV-3.2.0
link_directories("/home/ubuntu/opencv-3.2.0/lib") # directory of OpenCV-3.2.0
add_executable(cv_io io.cpp)
target_link_libraries(cv_io ${OpenCV_LIBS})
Now, when I run this little piece of code,
#include <iostream>
#include "opencv2/core/version.hpp"
int main(int argc, char ** argv)
{
std::cout << "OpenCV version: "
<< CV_MAJOR_VERSION << "."
<< CV_MINOR_VERSION << "."
<< CV_SUBMINOR_VERSION
<< std::endl;
return 0;
}
I get
OpenCV version: 3.2.0
instead of
OpenCV version 2.4.13
So everything seems to be in order, except when I start running some actual OpenCV functions such as:
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
int main()
{
cv::Mat img = cv::imread("ferrari.jpg");
cv::Mat dst;
cv::Sobel(img, dst, CV_32F, 1, 1);
cv::imwrite("ferrari_sobel.png", dst);
cv::VideoCapture input(0);
}
I get all these undefined reference errors:
CMakeFiles/cv_io.dir/io.cpp.o: In function main':
io.cpp:(.text+0x40): undefined reference tocv::imread(cv::String
const&, int)' io.cpp:(.text+0xd4): undefined reference to
cv::imwrite(cv::String const&, cv::_InputArray const&,
std::vector<int, std::allocator<int> > const&)'
CMakeFiles/cv_io.dir/io.cpp.o: In functioncv::String::String(char
const*)':
io.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x40):
undefined reference to cv::String::allocate(unsigned long)'
CMakeFiles/cv_io.dir/io.cpp.o: In functioncv::String::~String()':
io.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x10): undefined
reference to cv::String::deallocate()' CMakeFiles/cv_io.dir/io.cpp.o:
In functioncv::String::operator=(cv::String const&)':
io.cpp:(.text.ZN2cv6StringaSERKS0[ZN2cv6StringaSERKS0]+0x2c):
undefined reference to `cv::String::deallocate()' collect2: error: ld
returned 1 exit status CMakeFiles/cv_io.dir/build.make:121: recipe for
target 'cv_io' failed make2: * [cv_io] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/cv_io.dir/all'
failed make1: * [CMakeFiles/cv_io.dir/all] Error 2 Makefile:83:
recipe for target 'all' failed make: *** [all] Error 2
Does anyone know how to solve this problem? I think the problem is that I'm still not linking all the libraries properly in the CMakeLists.txt. Also, I found a comment below this article mentioning something similar to what I'm experiencing, but I do not understand the page containing the solution it is referring to. I'm very new to OpenCV and CMake, so hopefully you can provide me instructions that are as explicit as possible. I've been stuck on this for forever, so any help is highly appreciated! Thank you very much!
I have a working CMakelists.txt for almost the same configuration as you describe except that I am running a dauntingly old Ubuntu 12.04 (its not my own computer).
I believe your problem comes from this line:
find_package(OpenCV REQUIRED)
Which gives you access to your distribution's OpenCV 2.4. Then you are linking against the manually installed 3.2.x version. So problems arise as soon as the interface of a function you use has changed between the two version. Your first piece of code run by chance I think.
Here is my CMakeList.txt:
cmake_minimum_required(VERSION 2.8)
project(demo)
find_package(OpenCV 3.2 REQUIRED PATHS "/path/to/OCV3.2/install/dir/")
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})
If you do not want to commit to your repository the hardcoded path to your install of OpenCV 3.2 you can refine this CMakeList.txt by changing the find_package line to:
if(DEFINED ENV{OPENCV_INSTALL_DIR})
find_package(OpenCV 3.2 REQUIRED PATHS $ENV{OPENCV_INSTALL_DIR})
else()
message("OPENCV_INSTALL_DIR not set, searching in default location(s)")
find_package(OpenCV 3.2 REQUIRED)
endif(DEFINED ENV{OPENCV_INSTALL_DIR})
Then you just have to define the variable OPENCV_INSTALL_DIR before running cmake. I do that by exporting it from my .bashrc
While you are setting up the include files to use the newly installed OpenCV headers, you are linking against the opencv on your system path. The version is just stored in the headers, which is why it's outputting the expected version but failing to link. Try setting the variable "OpenCV_DIR" to the location of the OpenCVConfig.cmake file you want to use before running the find module.
See the documentation for your version of cmake here. Note the two modes that find_package can be run in (Module or Config mode).
Unless you are linking to static libraries, you will still have problems running your library / application. To fix that, you'll need to add the libraries to your LD_LIBRARY_PATH.
Indeed, you're linking against system-default version of OpenCV.
Reconfigure your project by calling CMake with right path to file OpenCVConfig.cmake.
cmake /path/to/your/sources -DOpenCV_DIR=/home/ubuntu/opencv-3.2.0
Find file CMakeCache.txt in your build directory. It contains internal CMake variables, OpenCV paths are also there.
Also, it is incorrect to hardcode paths. Use include_directories(${OPENCV_INCLUDE_DIRS})
And quotation from OpenCVConfig.cmake
- OpenCV_LIB_DIR : The directory(es) where lib files are. Calling LINK_DIRECTORIES
with this path is NOT needed.

C++ undefined reference for libtins library

I installed the libtins package for C++, by building it as described and adding #include <tins/tins.h> to the header of the example file. I ran ldconfig but building always throws a Undefined reference error:
CMakeFiles/lts.dir/main.cpp.o: In function `main':
/home/patrick/ClionProjects/lts/main.cpp:6: undefined reference to `Tins::EthernetII::EthernetII(Tins::HWAddress<6ul, unsigned char> const&, Tins::HWAddress<6ul, unsigned char> const&)'
/home/patrick/ClionProjects/lts/main.cpp:7: undefined reference to `Tins::IPv4Address::IPv4Address(char const*)'
/home/patrick/ClionProjects/lts/main.cpp:7: undefined reference to `Tins::IPv4Address::IPv4Address(char const*)'
/home/patrick/ClionProjects/lts/main.cpp:7: undefined reference to `Tins::IP::IP(Tins::IPv4Address, Tins::IPv4Address)'
/home/patrick/ClionProjects/lts/main.cpp:8: undefined reference to `Tins::TCP::TCP(unsigned short, unsigned short)'
/home/patrick/ClionProjects/lts/main.cpp:10: undefined reference to `Tins::PDU::inner_pdu(Tins::PDU*)'
/home/patrick/ClionProjects/lts/main.cpp:12: undefined reference to `Tins::PDU::inner_pdu(Tins::PDU*)'
CMakeFiles/lts.dir/main.cpp.o: In function `Tins::EthernetII::~EthernetII()':
/usr/local/include/tins/ethernetII.h:46: undefined reference to `vtable for Tins::EthernetII'
/usr/local/include/tins/ethernetII.h:46: undefined reference to `Tins::PDU::~PDU()'
collect2: error: ld returned 1 exit status
make[3]: *** [lts] Error 1
I am using CLion as IDE and the following cmake file:
cmake_minimum_required(VERSION 3.5)
project(lts)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -ltins")
set(SOURCE_FILES main.cpp)
add_executable(lts ${SOURCE_FILES})
Any Idea what could be the problem?
There's a couple of things to address in your question. First, let CMake find the location of the libtins library for you, then use target_link_libraries() to add it to your lts executable. In the CMake sample in your question, you are specifying library options in the compiler flags variable (CMAKE_CXX_FLAGS). The other thing is to also let CMake handle setting the appropriate compiler flags for C++11 instead of manually adding them to CMAKE_CXX_FLAGS (which would only take care of the compiler part but not the linker). The following should do what you want in a robust, platform independent way:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(lts)
find_library(TINS_LIBRARY tins)
set(SOURCE_FILES main.cpp)
add_executable(lts ${SOURCE_FILES})
target_link_libraries(lts "${TINS_LIBRARY}")
I recommend you read the CMake docs for find_library() and target_link_libraries() to understand what those commands do. For the C++11 stuff, I recommend this blog article for a more cohesive explanation than what the CMake docs give you (disclosure: I wrote the article).
The CMake variable CMAKE_CXX_FLAGS is for the compiler flags, not for libraries.
To add a library you should use target_link_libraries:
target_link_libraries(lts tins)
If you want all targets to link to the same library, you should add it to the list CMAKE_STANDARD_LIBRARIES.
A small tip on debugging these kind of things: Either set CMAKE_VERBOSE_MAKEFILE or use make VERBOSE=1 to inhibit the normal CMake build output, and instead display the normal make output, which will show you all commands make runs, and all flags and options used for each and every program. That way you will be able to see the -ltins flag being passed as a compiler flag, but not as a linker flag or library.
The compiler should -ltins. Refer to CMake manual on how to add library dependencies.
From the Download page of libtins:
In order to link your application with libtins on GCC or clang, use the -ltins flag
Simply add the following line in your CMake file:
target_link_libraries(PROJECT_NAME -ltins)

Undefined Python references in C++ using CMake

I am trying to compile a c++ project referencing Python using CMake. I am using Cygwin and I have Python2.7 source files in Cygwin.
For example:
PyObject *l = PyList_New(0);
Online help suggested I add the -lpython2.7 linker flag. Am I not adding this correctly in CMake? Otherwise why can I still not use the Python library and how might I fix this?
The compile line:
C:\cygwin64\bin\cmake.exe --build "C:\Users\...\.clion10\system\cmake\generated\3e6845d6\3e6845d6\Release" --target projectname -- -j 4
The CMakeList.txt file:
cmake_minimum_required(VERSION 2.8.4)
project(projectname)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lpython2.7")
set(SOURCE_FILES
src/cpp/...
src/cpp/...
src/cpp/..
src/cpp/...
src/cpp/...)
add_executable(projectname ${SOURCE_FILES})
The errors...
CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaeb4): undefined reference to `PyDict_New'
CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaeb4): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `PyDict_New'
CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaec4): undefined reference to `PyList_New'
CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaec4): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `PyList_New'
CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaf0d): undefined reference to `PyDict_New'
CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaf0d): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `PyDict_New'
CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaf25): undefined reference to `PyString_FromString'
...and so on....
You misunderstand CMake's way: before use something you ought to find it! I.e. make sure that everything you need to build your package is available and usable at build host. Otherwise that would be not good to waste a (compile) time (say 2 hours) and then get an error that some header/library/executable not found. So, at CMake run time you'd better to be sure that everything you need is here. To do so, CMake have a lot of tools.
Consider your particular case: you need to find Python libraries otherwise build is not possible. To do so, you ought to use find_package like this:
find_package(PythonLibs REQUIRED)
Take a look to documentation and provide other options (like version) if you need. You shouldn't use hardcoded paths in your CMakeLists.txt, otherwise your project wouldn't be really portable (and most probably you'll be the only who can build it w/o a lot of problems). Instead Python libs finder module will provide variables you need to use later, or failed w/ error if nothing has found.
If CMake ends w/o errors, you may use found Python libs. First of all you need to update #include paths:
include_directories(${PYTHON_INCLUDE_DIRS})
Then tell to linker that your executable projectname needs to be linked w/ Python libs:
add_executable(projectname ${SOURCE_FILES})
target_link_libraries(projectname ${PYTHON_LIBRARIES})
And again, try to avoid to modify CMAKE_CXX_FLAGS (and others) directly -- there are bunch of calls to do that globally and/or per target. Some of them are:
add_definitions to define/undefine macros
include_directories to update #include paths
add_compile_options to add other compiler options
link_directories to update linker search paths

undefined function during cmake linking local file

I'm trying to compile a small Qt example to an executable (with the test.cpp containg main) and later add support for compiling the non-test.cpp code to its own library. I was using qmake, but now I'm trying to use cmake following the example here. Unfortunately, I'm getting a linking error. Looks like the constructors, which I define in the cpp files that are compiling aren't being found at link time.
$ make
Linking CXX executable test
CMakeFiles/test.dir/attribute_editor.cpp.o: In function `AttributeEditor::AttributeEditor(QWidget*)':
attribute_editor.cpp:(.text+0x2a): undefined reference to `vtable for AttributeEditor'
CMakeFiles/test.dir/bindable.cpp.o: In function `Bindable::Bindable(QObject*)':
bindable.cpp:(.text+0x50): undefined reference to `vtable for Bindable'
CMakeFiles/test.dir/bindable.cpp.o: In function `AttributeObject::AttributeObject()':
bindable.cpp:(.text._ZN15AttributeObjectC2Ev[_ZN15AttributeObjectC5Ev]+0x24): undefined reference to `vtable for AttributeObject'
collect2: error: ld returned 1 exit status
make[2]: *** [test] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
This is my cmake file...
cmake_minimum_required(VERSION 2.8)
# http://qt-project.org/quarterly/view/using_cmake_to_build_qt_projects
PROJECT(qattrs)
FIND_PACKAGE(Qt4 REQUIRED)
SET(QT_USE_QTSCRIPT TRUE)
SET(qattrs_SOURCES test.cpp attribute_editor.cpp bindable.cpp)
SET(qattrs_HEADERS bindable.h attribute_editor.h)
QT4_WRAP_CPP(qattrs_HEADERS_MOC ${qattrs_HEADERS})
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
ADD_EXECUTABLE(test ${qattrs_SOURCES} ${qattrs_HEADER_MOC})
TARGET_LINK_LIBRARIES(test ${QT_LIBRARIES})
I know very little about cmake, but I assume it's not including the .o files during linking.
The entire code (five or six files) is on github.
It cannot find a vtable for something - this means it is looking for the entries a type would put in it's vtable if it existed, because it is a linker. (It is not talking about a type lacking a vtable at runtime because that'd make no sense at all, I don't even like that sentence)
Somewhere you have a pure-virtual method you have never defined.
That code is REALLY hard to read and reading it I couldn't see any virtuals even in bindable.h, this means there's a pure virtual function in the parent class and you never implement it, that is your error.
Well more specifically you never implement 3 of them.
I'd feel bad for not saying: wxWidgets FTW :P
I found the answer on another question. It wasn't in the example Qt doc, but I just needed to set another environment variable.
set(CMAKE_AUTOMOC ON)
Why am I getting "undefined reference to vtable..." errors when linking this Qt 5.0 application?

'undefined reference to' with cmake

So, I am trying to write an applet for school, and part of it requires using cmake. I have two different classes contained in their own files, and I use them as part of the main class. I have included their headers as such in the main project header:
#include /path/to/header/1.h
#include /path/to/header/2.h
The problem I have is that when I run make after I've run cmake, I get undefined reference errors for any instance in which I try to use one of the two libraries. I know that it has to do with linker errors, but since I'm new to cmake, I'm not exactly sure what the proper way to use TARGET_LINK_LIBRARIES would be.
EDIT
After linking/associating my libraries, I have the following:
CMakeLists.txt:
# A name for the project
project(plasma-engine-gdrive)
# Find the required libaries
find_package(KDE4 REQUIRED)
include(KDE4Defaults)
add_definitions (${QT_DEFINITIONS} ${KDE4_DEFINITIONS}, -std=c++0x)
include_directories(
${CMAKE_SOURCE_DIR}
${CMAKE_BINARY_DIR}
${KDE4_INCLUDES}
./include
./lib
)
set (GOOGLE_LIBS include/atom_helper.h include/util/string_utils.h include/client/doc_list_service.h include/client/service.h)
set (GOOGLE_SRCS include/atom_helper.cc include/util/string_utils.cc include/client/doc_list_service.cc include/client/service.cc)
# We add our source code here
set(gdrive_engine_SRCS gdriveengine.cpp)
add_library (DataWrapper include/DataWrapper.cpp include/DataWrapper.h)
add_library (GData ${GOOGLE_SRCS} ${GOOGLE_LIBS})
# Now make sure all files get to the right place
kde4_add_plugin(plasma_engine_gdrive ${gdrive_engine_SRCS})
target_link_libraries(plasma_engine_gdrive
GData
DataWrapper
${KDE4_KDECORE_LIBS}
${KDE4_PLASMA_LIBS})
install(TARGETS plasma_engine_gdrive
DESTINATION ${PLUGIN_INSTALL_DIR})
install(FILES plasma-engine-gdrive.desktop
DESTINATION ${SERVICES_INSTALL_DIR})
There are also too many errors to put here. Here's a few:
/usr/include/c++/4.6/bits/stl_map.h:467: undefined reference to `Glib::ustring::ustring()'
lib/libGData.a(atom_helper.o): In function `pair<Glib::ustring, Glib::ustring, void>':
/usr/include/c++/4.6/bits/stl_pair.h:132: undefined reference to `Glib::ustring::ustring(Glib::ustring const&)'
/usr/include/c++/4.6/bits/stl_pair.h:132: undefined reference to `Glib::ustring::ustring(Glib::ustring const&)'
lib/libGData.a(atom_helper.o): In function `pair<Glib::ustring, Glib::ustring>':
/usr/include/c++/4.6/bits/stl_pair.h:137: undefined reference to `Glib::ustring::ustring(Glib::ustring const&)'
/usr/include/c++/4.6/bits/stl_pair.h:137: undefined reference to `Glib::ustring::ustring(Glib::ustring const&)'
I was working with some others and I got it compiled!
All I really needed was to provide the name of the library and put it into target_link_libraries like so:
target_link_libraries(plasma_engine_gdrive
DataWrapper
GData
xml++-2.6
curl
glibmm-2.4
${KDE4_KDECORE_LIBS}
${KDE4_PLASMA_LIBS})
My dad, being completely unfamiliar with CMake, went and dug out the link.txt within the CMake build structure. There, he just added the following after the -o flag:
-lxml++-2.6
-lcurl
-lglibmm-2.4
When I saw that, I thought I might try to do that linking through CMakeLists.txt -- and it worked.