xerces c++ and cmake - c++

i tryng to build a small example of Xerces with xerces c++ 3.1 and cmake, but i and only getting linkings problems.
This is my cmkelists.txt:
//============================================================================
project(ConfiguradorXerces)
cmake_minimum_required(VERSION 2.8)
include_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src)
link_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src/.libs)
link_directories (/home/ricardo/Desktop/librerias/xerces/xerces-c-3.1.1/src/)
set ( XercesLib xerces-c )
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${XercesLib})
//==============================================
//===============================================
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
using namespace xercesc;
using namespace std;
int main()
{
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
//==============================================
and this is my console output:
CMakeFiles/ConfiguradorXerces.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x25): undefined reference to `xercesc_3_1::XMLUni::fgXercescDefaultLocale'
main.cpp:(.text+0x2a): undefined reference to `xercesc_3_1::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_1::PanicHandler*, xercesc_3_1::MemoryManager*)'
main.cpp:(.text+0x2f): undefined reference to `xercesc_3_1::XMLPlatformUtils::Terminate()'
CMakeFiles/ConfiguradorXerces.dir/main.cpp.o:(.gcc_except_table+0x10): undefined reference to `typeinfo for xercesc_3_1::XMLException'
collect2: error: ld returned 1 exit status
make[2]: *** [ConfiguradorXerces] Error 1
make[1]: *** [CMakeFiles/ConfiguradorXerces.dir/all] Error 2
make: *** [all] Error 2
16:28:55: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project ConfiguradorXerces (target: Desktop)
When executing step 'Make'
//
i was thinking trhat my cmakeLsits.txt was not complete, there is an especial setup that it has to be done??
thx in advance

I am pretty sure that target_link_libraries() macro accepts a target as its first parameter:
target_link_libraries(<target> [item1 [item2 [...]]]
[[debug|optimized|general] <item>] ...)
And you have forgot to specify it. So instead of target_link_libraries(${XercesLib}), try target_link_libraries(${PROJECT_NAME} ${XercesLib}).
Hopefully, that solves it.

Related

Linker Error: Undefined reference to `ThreadPool::ThreadPool()'

I have a class threadpool. I have a header file threadpool.h and the implementation threadpool.cc. When trying to I initialize the threadpool like so: ThreadPool *tp = new ThreadPool(); I get the following linker error:
/usr/bin/ld: ../src/libmapreducelib.a(mapreduce_impl.cc.o): in function `Master::Master(MapReduceSpec const&, std::vector<FileShard, std::allocator<FileShard> > const&)':
/aos-pr4/src/master.h:43: undefined reference to `ThreadPool::ThreadPool()'
collect2: error: ld returned 1 exit status
make[2]: *** [test/CMakeFiles/mrdemo.dir/build.make:127: bin/mrdemo] Error 1
make[1]: *** [CMakeFiles/Makefile2:330: test/CMakeFiles/mrdemo.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
Here is my project source hierarchy:
src/
>CMakeLists.txt
>threadpool.h
>threadpool.cc
>otherfiles
CMakeLists.txt
Below is my threadpool.h, threadpool.cc and CMakeLists.txt file.
// in theadpool.h
#ifndef THREADPOOL_H
#define THREADPOOL_H
#pragma once
class ThreadPool {
public:
using Task = std::function<void()>;
// constructor, init static number of workers to be added to queue
ThreadPool()
ThreadPool(int numOfWorkers);
virtual ~ThreadPool();
....
}
#endif
// in threadpool.cc
#include "threadpool.h"
ThreadPool::ThreadPool(){
// commented out
}
....
I even ensured my cmakelist library includes the threadpool.h.
//CMakeLists.txt
project(mapreducer)
include(GenerateProtos.cmake)
add_library(
mapreducelib #library name
mapreduce.cc mapreduce_impl.cc #sources
master.h mapreduce_spec.h file_shard.h threadpool.h) #headers
target_link_libraries(mapreducelib p4protolib)
target_include_directories(mapreducelib PUBLIC ${MAPREDUCE_INCLUDE_DIR})
add_dependencies(mapreducelib p4protolib)
I believe it should be fine and I've used my threadpool in another project. It must be the project hierarchy.

Tensorflow c++ api undefined reference to `tflite::DefaultErrorReporter()'

I successfully built tensorflow_cc on Ubuntu 16.04 with a static build.
I was testing a small code I took from tflite guide:
$> cat test1.cpp
#include <cstdio>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
using namespace tflite;
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "minimal \n");
return 1;
}
const char* filename = argv[1];
std::unique_ptrtflite::FlatBufferModel model =
tflite::FlatBufferModel::BuildFromFile(filename);
return 0;
}
I am getting this error:
[ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o
[100%] Linking CXX executable example
CMakeFiles/example.dir/example.cpp.o: In function main': example.cpp:(.text+0x57):
undefined reference to tflite::DefaultErrorReporter()'
example.cpp:(.text+0x6d): undefined reference to
tflite::FlatBufferModel::BuildFromFile(char const*, tflite::ErrorReporter*)'
CMakeFiles/example.dir/example.cpp.o: In function
std::default_deletetflite::FlatBufferModel::operator()(tflite::FlatBufferModel*) const':
example.cpp
(.text.ZNKSt14default_deleteIN6tflite15Flat
BufferModelEEclEPS1[ZNKSt14default_deleteIN6tflite15FlatBufferModelEEclEPS1]+0x1e):
undefined reference to `tflite::FlatBufferModel::~FlatBufferModel()'
collect2: error: ld returned 1 exit status
CMakeFiles/example.dir/build.make:85: recipe for target 'example' failed
make[2]: *** [example] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/example.dir/all' failed
make[1]: *** [CMakeFiles/example.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Any suggestions on how to proceed?
It's answered on https://github.com/tensorflow/tensorflow/issues/36661
By some strange reason, you need to provide your object file first than tflite library to your linker.
FYI https://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking

how to get python.h working with mingw gcc compiler

i have included the path to python.h(C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include) file and gave the library path of the project to "C:\Users\AppData\Local\Programs\Python\Python37-32\Lib"
and my code is as pasted below:-
#include <iostream>
#include <Python.h>
using namespace std;
int main(int argc, char **argv)
{
PyObject *pInt;
Py_Initialize();
PyRun_SimpleString("print('Hello world from embedded python!!!\n')");
Py_Finalize();
cout<<"now i am finally out of the python"<<endl;
return 0;
}
when i compile the program there is no error but when i build the project i get the following erros
C:/Users/Documents/CppWorkspace/Cpp_in_python/main.cpp:8: undefined reference to `_imp__Py_Initialize'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Documents/CppWorkspace/Cpp_in_python/main.cpp:9: undefined reference to `_imp__PyRun_SimpleStringFlags'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/Users/Documents/CppWorkspace/Cpp_in_python/main.cpp:10: undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[1]: *** [Debug/Cpp_in_python] Error 1
mingw32-make.exe: *** [All] Error 2
Cpp_in_python.mk:78: recipe for target 'Debug/Cpp_in_python' failed
mingw32-make.exe[1]: Leaving directory 'C:/Users/vinay/Documents/CppWorkspace/Cpp_in_python'
Makefile:4: recipe for target 'All' failed
====4 errors, 0 warnings====

cmake linking error 2 [duplicate]

This question already has answers here:
Error with multiple definitions of function
(4 answers)
Closed 4 years ago.
I am trying to compile a simple code using cmake and I getting an error. The code and cmake file are as below. The test.cpp is the main file in which i have directly included test1.cpp. I have also included my CMake file and the error that I am getting on performing make.
test.cpp
#ifndef _IOSTREAM_
#include<iostream>
#endif
#include"test1.cpp"
using namespace std;
int main()
{
printing("hello");
return 0;
}
test1.cpp
#ifndef _IOSTREAM_
#include<iostream>
#endif
#include<string>
using namespace std;
void printing(string s)
{
cout<<s<<endl;
return;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
project(test)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++11)
add_executable(test test.cpp test1.cpp)
Error
CMakeFiles/test.dir/test1.cpp.o: In function
printing(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >):
/home/vatsal/Desktop/test/test1.cpp:(.text+0x0): multiple definition
of printing(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >)
CMakeFiles/test.dir/test.cpp.o:/home/vatsal/Desktop/test/test.cpp:
(.text+0x0): first defined here
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
CMakeFiles/test.dir/build.make:98: recipe for target test failed
make[2]: *** [test] Error 1
CMakeFiles/Makefile2:67: recipe for target CMakeFiles/test.dir/all
failed
make[1]: *** [CMakeFiles/test.dir/all] Error 2
Makefile:83: recipe for target all failed
make: *** [all] Error 2
It happens because include cpp file is a bad idea.
After preprocessor work you'll get two defenition of void printing(string s) The first one is in test.cpp because you've incleded test1.cpp and the second one is in test1.cpp.
Solution is to create test1.h which contain declaration of function:
#include<iostream>
#include<string>
using namespace std;
void printing(string s);
Then fix test1.cpp:
#include "test1.h"
using namespace std;
void printing(string s)
{
cout<<s<<endl;
return;
}
And finaly replace #include"test1.cpp" with #include"test1.h" in test.cpp

cLion + QT - Linker Error when using Q_OBJECT

I get a linker error if I put C_OBJECT into my header.
Currently I copy my QT dll library to my project, otherwise, I get exit code error as I don't know how to add QT library in cMake yet... Not sure if that is causing the error. Maybe I need to add Q_OBJECT in CMakeList.txt somehow?
Here is the sample code:
main.cpp
#include <iostream>
#include <QtWidgets/QApplication>
#include "testWidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
testWidget w;
w.show();
return a.exec();
}
testWidget.h
#ifndef QOBJECTCRASH_TESTWIDGET_H
#define QOBJECTCRASH_TESTWIDGET_H
#include <QtWidgets/QMainWindow>
class testWidget : public QMainWindow
{Q_OBJECT //this crashes the app. Remove it and all runs.
public:
testWidget(QWidget *parent = Q_NULLPTR);
};
#endif //QOBJECTCRASH_TESTWIDGET_H
testWidget.cpp
#include "testWidget.h"
testWidget::testWidget(QWidget *parent): QMainWindow(parent) {
setWindowTitle("Test Q_OBJECT");
setGeometry(500,500,500,500);
}
CMakeList.txt
cmake_minimum_required(VERSION 3.8)
project(QObjectCrash)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_PREFIX_PATH "C:\\Qt\\5.9\\mingw53_32")
set(SOURCE_FILES main.cpp testWidget.cpp testWidget.h)
add_executable(QObjectCrash ${SOURCE_FILES})
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Gui REQUIRED)
qt5_use_modules(QObjectCrash Core Widgets Gui)
target_link_libraries(QObjectCrash Qt5::Widgets Qt5::Core Qt5::Gui)
Error print from cLion:
CMakeFiles\QObjectCrash.dir/objects.a(main.cpp.obj): In function `ZN10testWidgetD1Ev':
D:/QObjectCrash/testWidget.h:11: undefined reference to `vtable for testWidget'
D:/QObjectCrash/testWidget.h:11: undefined reference to `vtable for testWidget'
CMakeFiles\QObjectCrash.dir/objects.a(testWidget.cpp.obj): In function `ZN10testWidgetC2EP7QWidget':
D:/QObjectCrash/testWidget.cpp:7: undefined reference to `vtable for testWidget'
D:/QObjectCrash/testWidget.cpp:7: undefined reference to `vtable for testWidget'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\QObjectCrash.dir\build.make:126: recipe for target 'QObjectCrash.exe' failed
mingw32-make.exe[3]: *** [QObjectCrash.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/QObjectCrash.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/QObjectCrash.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/QObjectCrash.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/QObjectCrash.dir/rule] Error 2
mingw32-make.exe: *** [QObjectCrash] Error 2
Makefile:117: recipe for target 'QObjectCrash' failed
Thanks for help!