首页 > 解决方案 > 在 Ubuntu 上的 Eclipse IDE 上的 Yocto 插件上未定义对“pthread_create”的引用

问题描述

我正在 Eclipse IDE 中为使用 Yocto 制作的元工具链进行交叉编译测试,用于 arm cortex A9 处理器。在执行成功运行的 hello world 测试后,我创建了一个基本程序来测试 pthread。

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <time.h>
    #include <pthread.h>

    #define MILLION 1000000         /* one million = 10^6*/
    #define sec_to_nsec 1000000000  /* ns to s conversion = 10^9 */
    #define FILENAME "Schd.txt"
    #define FLUSH_TIME 10.0
    #define SIG_LLP_TIMER       SIGRTMIN+1

    int     isr_idx;            /* counter of ISR occurred -- starts from 0 and increments at each interrupt*/

    volatile float  clk_k,              /* MY_CLOCK() value for the current sample*/
            clk_k_1;            /* MY_CLOCK() value for the previous sample*/

    /*clock and timer values*/
    struct itimerspec custom_itimerspec;
    timer_t timer_id;
    clockid_t USED_CLK;
    struct  timespec tv;

    float a_n;

    /*THREAD DATA*/
    pthread_t thread0;
    pthread_attr_t attr;
    struct sched_param param;
    using namespace std;



    void* thread_scheduler(){
        //function pointer
        //mainThread
        //make thread for scheduling
        //exit after max cycle
    }

    int main(void) 
    {

            cout << "Starting the program!" << endl; /* prints Hello World */

            cout<< "Creating a Thread to deploy" << endl;


            int  status;


            param.__sched_priority = 99;


                int retc;
                /*PTHREAD ATTR setup*/
                retc = pthread_attr_init(&attr);
                retc |= pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
                retc |= pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
                retc |= pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
                retc |= pthread_attr_setschedparam(&attr,&param);


                if (retc != 0) {
                    //fail
                    while(1){}
                }


                retc = pthread_create(&thread0, &attr, (void * (*)(void *))thread_scheduler, NULL);

            printf("Exiting here!");
            return 0;





    }

但是我收到了这个错误,未定义对“pthread_create”的引用,然后是一些错误。

尽管在进行了一些搜索后,我发现在配置和自动生成设置中添加“-pthread”命令可用于构建项目,如此所述。但是我很困惑为什么即使这个文件存在于项目资源管理器的下拉文件夹中的“包含”中,编译器也看不到这些文件。

标签: c++linuxeclipseyocto

解决方案


The error about undefined reference is coming from linking step, not from compiling and assembling step, compile step would look for header files and its rightly finding the pthread.h from sysroot include directory as you see as well. After compiling, it has to invoke the linker to create the executable binary and thats where it fails.

When linking it need to add libpthread to linker commandline so linker can find the pthread_create function and link it into final executable, this is usually done via specifying LDFLAGS which then get appended to linker invocation.

compiler driver ( gcc ) can be used to drive both compiling and linking steps. so when you add -pthread option to compiler and compiler is also used to perform linking then it translates this option into -lpthread to linker cmdline which would then find libpthread and link it in.


推荐阅读