Linker Error: Undefined reference to `ThreadPool::ThreadPool()' - c++

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.

Related

Issue with ros NodeHandle.subscribe with a class method

I am using ros melodic on Ubuntu18.04 and C++14 and am trying to subscribe to a topic using a callback function in a Derived class. I know this is possible as a base class as seen in the 2.3.2 Class Methods section of the ros wiki, but I am trying to modify that for a derived class. I would like to be able to have multiple Scan_matching derived classes for different algorithms, and for main class to be able to select one. Here is what I have:
class Scan_Matching
{
public:
void get_transform(const sensor_msgs::PointCloud2ConstPtr& cloud_msg);
};
class ICP : public Scan_Matching
{
public:
void get_transform(const sensor_msgs::PointCloud2ConstPtr& cloud_msg);
};
int main(){
...
ScanMatching* scan_matching;
scan_matching = new ICP(...);
ros::Subscriber sub = nh.subscribe(topic, 100, &Scan_Matching::get_transform, scan_matching);
...
}
I am confident this is possible but I am not sure if I am implementing this correctly as I
get a linking error from catkin:
[ 14%] Linking CXX executable /home/mike/documents/autonomous_sim/devel/lib/mike_av_stack/localization
CMakeFiles/mike_av_stack_node.dir/scripts/localization/localization.cpp.o: In function `main':
localization.cpp:(.text.startup+0x718): undefined reference to `Scan_Matching::get_transform(boost::shared_ptr<sensor_msgs::PointCloud2_<std::allocator<void> > const> const&)'
collect2: error: ld returned 1 exit status
mike_av_stack/CMakeFiles/mike_av_stack_node.dir/build.make:392: recipe for target '/home/mike/documents/autonomous_sim/devel/lib/mike_av_stack/localization' failed
make[2]: *** [/home/mike/documents/autonomous_sim/devel/lib/mike_av_stack/localization] Error 1
CMakeFiles/Makefile2:2493: recipe for target 'mike_av_stack/CMakeFiles/mike_av_stack_node.dir/all' failed
make[1]: *** [mike_av_stack/CMakeFiles/mike_av_stack_node.dir/all] Error 2
Makefile:145: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j12 -l12" failed
To me this seems like the subscribe function is sending my get_transform function a parameter that is not the type
const sensor_msgs::PointCloud2ConstPtr& cloud_msg
(or in other words)
const boost::shared_ptr<sensor_msgs::PointCloud2 const>& cloud_msg
but instead the type
boost::shared_ptr<sensor_msgs::PointCloud2_std::allocator<void > const> const&
I tested this by trying the following code which compiles fine.
void callback(const boost::shared_ptr<sensor_msgs::PointCloud2 const>& cloud_msg){
}
int main(){
...
ros::Subscriber sub = nh.subscribe(topic, 100, callback);
...
}
I think it might be because of the derived class and I am calling the incorrect overloaded subscriber function. Can someone help me understand what I am doing wrong, and how to fix? Thank you

Why can't I use multithreading in my project?

I want to start with multithreading, but my clion IDE (v. 2019.3.6) does not recognize created threads.
I also try to use threads from boost library, but there is also common error ("undefined reference to boost::threads" is there). I use c++14 in my clion and mingw compilator.
I compiled boost library using g++.
Code I try to run:
#include <iostream> // std::cout
#include <boost/thread.hpp>
void foo(){
// do stuff...
}
void bar(int x){
// do stuff...
}
int main(){
boost::thread t1(bar, 1);
t1.join();
return 0;
}
or:
#include <iostream> // std::cout
#include <thread>
void foo(){
// do stuff...
}
void bar(int x){
// do stuff...
}
int main(){
std::thread t1(bar, 1);
t1.join();
return 0;
}
errors which it generates:
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::thread::start_thread()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:182: undefined reference to `boost::thread::start_thread_noexcept()'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::thread::~thread()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:257: undefined reference to `boost::thread::detach()'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::thread::join()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:759: undefined reference to `boost::thread::get_id() const'
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:759: undefined reference to `boost::this_thread::get_id()'
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:762: undefined reference to `boost::thread::join_noexcept()'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(int), boost::_bi::list1<boost::_bi::value<int> > > >::~thread_data()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:94: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
CMakeFiles\client.dir/objects.a(client.cpp.obj):client.cpp:(.rdata$.refptr._ZTVN5boost6detail16thread_data_baseE[.refptr._ZTVN5boost6detail16thread_data_baseE]+0x0): undefined reference to `vtable for boost::detail::thread_data_base'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\client.dir\build.make:86: client.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/client.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: client] Error 2
or errors thread from std::thread
C:\Users\Admin\Desktop\client\client.cpp: In function 'int main()':
C:\Users\Admin\Desktop\client\client.cpp:15:10: error: 'thread' is not a member of 'std'
std::thread t1(bar, 1);
^~~~~~
C:\Users\Admin\Desktop\client\client.cpp:15:10: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
C:\Users\Admin\Desktop\client\client.cpp:5:1:
+#include <thread>
C:\Users\Admin\Desktop\client\client.cpp:15:10:
std::thread t1(bar, 1);
^~~~~~
C:\Users\Admin\Desktop\client\client.cpp:16:5: error: 't1' was not declared in this scope
t1.join();
^~
C:\Users\Admin\Desktop\client\client.cpp:16:5: note: suggested alternative: 'tm'
t1.join();
^~
tm
mingw32-make.exe[3]: *** [CMakeFiles\client.dir\build.make:63: CMakeFiles/client.dir/client.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/client.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: client] Error 2
Why thread is not memeber of std::thread?
I have attached proper header
Is this possible that mingw does not support multithreading?
Maybe someone had problem like this and will be able to help.
The mingw didn't recognize thread library.
I changed version of compilator to another and everything works nice.

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====

Cocos2D-X compile Linux Error: Undefined Reference

I'm new to cocos2D-X (v 3.6), I'm using Linux to compile and run my project. When I try the following command line:
cocos compile -p linux
It gives me errors:
Linking CXX executable bin/MyGame
CMakeFiles/MyGame.dir/Classes/AppDelegate.cpp.o: In function `AppDelegate::applicationDidFinishLaunching()':
/home/caisar/MyCompany/MyGame/Classes/AppDelegate.cpp:53: undefined reference to `GraphicsScene::createScene()'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/MyGame] Error 1
make[1]: *** [CMakeFiles/MyGame.dir/all] Error 2
make: *** [all] Error 2
Error running command, return code: 2
This is my GraphicsScene.h:
#ifndef __GRAPHICS_SCENE_H__
#define __GRAPHICS_SCENE_H__
#include "cocos2d.h"
class GraphicsScene : public cocos2d::Layer{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);
CREATE_FUNC(GraphicsScene);
};
#endif
And this is my GraphicsScene.cpp:
#include "GraphicsScene.h"
USING_NS_CC;
Scene* GraphicsScene::createScene(){
auto scene = Scene::create();
auto layer = GraphicsScene::create();
scene->addChild(layer);
return scene;
}
bool GraphicsScene::init(){
if(!Layer::init()){
return false;
}
auto sprite = Sprite::create("autobot.png");
sprite->setPosition(0,0);
this->addChild(sprite, 0);
return true;
}
void GraphicsScene::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
Is there anything else I need to add?
I also had this problem today and solved it by editing the PROJECT_PATH/CMakeLists.txt file. You must include the *.cpp and *.h filepaths in the GAME_SRC and GAME_HEADERS sections. For example, in your case it must be something like this:
set(GAME_SRC
Classes/AppDelegate.cpp
Classes/HelloWorld.cpp
Classes/GraphicsScene.cpp
${PLATFORM_SPECIFIC_SRC}
)
set(GAME_HEADERS
Classes/AppDelegate.h
Classes/HelloWorld.h
Classes/GraphicsScene.h
${PLATFORM_SPECIFIC_HEADERS}
)
Hope that it works!
I guess the linker did not find the .cpp source file for GraphicsScene::createScene(). And you should add GraphicsScene.cpp to Android.mk file. Such as i did that for my project on windows.
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/Graphic_Image.cpp \
Hope it help.
Thanks.

xerces c++ and cmake

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.