我正在尝试构建CMake项目,下面是我正在尝试做的示例代码。
我正在开发两个头文件,我想使用线程头从类 Loader 中调用类 Processor 中的成员函数。这是代码,以及CMake,以及运行make
命令后收到的错误消息。
CMakeLists.txt
cmake_minimum_required(VERSION 3.2.2)
project(c-plus-assignment)
## C++11 Flags
add_compile_options(-std=c++11)
add_compile_options(-pthread)
add_compile_options(-Werror=return-type) # error on missing return type
add_compile_options(-Werror=format) # error on wrong printf formats
add_compile_options(-Werror=parentheses) # error when using ambiguous syntax
include_directories(${PROJECT_SOURCE_DIR}/include)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_executable(main_app
main.cpp
src/Processor.cpp
src/Loader.cpp
src/Logger.cpp)
Loader.hpp
#pragma once
#include <string>
#include <c-assignment/Processor.hpp>
using namespace std;
class Loader
{
private:
Processor *Processor_obj; // creating an object out of the Processor class
std::thread t1;
Processor boo;
public:
Loader();
~Loader();
void thr(int time);
string usr_input;
void read_usr_input(void);
};
Processor.hpp
#pragma once
#include <string>
#include <thread>
using namespace std;
class Processor
{
public:
bool status; //status of processing true = free, false = busy
float wait_amount; //variable to store the random generated float
Processor();
void process(string);
};
Loader.cpp
#include <c-assignment/Loader.hpp>
#include <c-assignment/Logger.hpp>
#include <c-assignment/Processor.hpp>
#include <iostream>
#include <thread>
Loader::Loader()
{
Processor_obj = new Processor(); // creating an object out of the Processor class
}
Loader::~Loader()
{
t1.join();
}
void Loader::read_usr_input(void)
{
std::cout << "Please enter a command" << '\n';
std::cin >> Loader::usr_input;
if (Processor_obj->status == true) //check if the Processor is busy or not
{
if (Loader::usr_input == "fc" || Loader::usr_input == "out")
{
//Processor_obj->process(Loader::usr_input);
Loader::t1 = std::thread(&Processor::process,Loader::boo,Loader::usr_input);
}
//else if (){}
//else if (){}
else
{
std::cout << "Unknown command" << '\n';
}
std::cout << '\n' << "you entered the command" << Loader::usr_input << '\n';
}
else {std::cout << "Processor class is busy" << '\n';}
}
Processor.cpp
#include <c-assignment/Processor.hpp>
#include <c-assignment/Logger.hpp>
#include <iostream>
#include <thread>
#include <chrono>
#include <string>
#include <random>
#include <ctime>
using namespace std;
Processor::Processor()
{
Processor::status = true;
}
void Processor::process(string usr_input)
{
srand(time(NULL));
Processor::wait_amount = ((float)rand() / RAND_MAX) * (3.00f - 1.00f) + 1.00f;
std::cout << "waiting time is" << (Processor::wait_amount)*1000 << "milliseconds" << '\n';
Processor::status = false;
std::this_thread::sleep_for(std::chrono::milliseconds((int)(Processor::wait_amount)*1000));
Processor::status = true;
std::cout << "/Processed/" << '\n';
}
main.cpp
#include <c-assignment/Processor.hpp>
#include <c-assignment/Loader.hpp>
#include <c-assignment/Logger.hpp>
int main()
{
while (true) {
Processor a;
Loader b;
Logger c;
//a.process("rfc");
b.read_usr_input();
}
return 0;
}
这是我运行make
时的输出:
[ 20%] Linking CXX executable main_app
CMakeFiles/main_app.dir/src/Loader.cpp.o: In function `std::thread::thread<void (Processor::*)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >), Processor&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>(void (Processor::*&&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >), Processor&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
Loader.cpp:(.text._ZNSt6threadC2IM9ProcessorFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEJRS1_RS7_EEEOT_DpOT0_[_ZNSt6threadC5IM9ProcessorFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEJRS1_RS7_EEEOT_DpOT0_]+0xb4): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/main_app.dir/build.make:172: recipe for target 'main_app' failed
make[2]: *** [main_app] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main_app.dir/all' failed
make[1]: *** [CMakeFiles/main_app.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
答案 0 :(得分:1)
myList = newMyList
输出的重要部分是链接器错误:make
。这表明链接器找不到该pthread函数的定义。您还没有链接 CMake中的pthread库,因此您应该尝试这样做以修复错误。在您对undefined reference to 'pthread_create'
的CMake调用之后,添加以下行:
add_executable()
这假设您对target_link_libraries(main_app PRIVATE Threads::Threads)
的调用正确找到了pthread库并填充了导入的目标Threads::Threads
。如果那不起作用,您也可以尝试直接链接到find_package(Threads REQUIRED)
库,而不是使用以下内容:
pthread