gpt4 book ai didi

C++ CMake 项目 : Recipe for target 'main_app' failed

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:01:43 25 4
gpt4 key购买 nike

<分区>

我正在尝试构建一个 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)

加载程序.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);
};

处理器.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);
};

加载程序.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';}
}

处理器.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';
}

主要.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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com