This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Where do I add the pthread library in KDevelop 4.3.1?

Tags: None
(comma "," separated)
newaroundhere
Registered Member
Posts
4
Karma
0
Hi.

I'm having a problem with threads in KDevelop and I'd appreciate any help.

This may sound like a dumb question to you, but please be patient, for I'm neither an expert in KDevelop, nor in C++, nor in Ubuntu.

I'm trying to run a C++ program that involves threads in KDevelop 4.3.1, but I get this error: undefined reference to `pthread_create'.

I'm using Ubuntu 12.04 LTS.

As far as I know, I'm getting that error because I haven't added the pthread library to my project (I did include the header), but I don't know where to do this.

This is my code:
Code: Select all
#include <iostream>
#include <pthread.h>
#include <time.h>

using namespace std;

int counter = 0;
bool running = true;

void * Func(void * arg)
{
    while(running)
    {
        counter++;
        sleep(2);
    }
    return 0;
}

int main(int argc, const char * argv[])
{
    int veces = 0;
    pthread_t id;
    cout << "Lanzando el primer hilo" << endl;
    pthread_create(&id, 0, Func, 0);

    while(running)
    {
        if (veces > 5)
        {
            running = false;
        }
        veces++;
        sleep(1);
    }

    pthread_join(id, 0);
    cout << "Variable global termina con: " << counter << endl;
    return 0;
}


So my question is: where do I add the pthread library to my project or where do I add some kind of argument (-lpthread, I'm guessing) to my project?

Thanks in advance.


Regards.
User avatar
scummos
Global Moderator
Posts
1175
Karma
7
OS
Hello!

How you do this depends on your build system (and is thus not really related to KDevelop). If you're using cmake, you should be able to solve this by putting something like this into your CMakeLists.txt:
Code: Select all
find_package (Threads)
target_link_libraries (myapp ${CMAKE_THREAD_LIBS_INIT})

(I didn't test that, tough ;P)
If you want to add plain flags, you can also do target_link_libraries(myapp pthread), but that's a bit dirty.

Greetings,
Sven


I'm working on the KDevelop IDE.
newaroundhere
Registered Member
Posts
4
Karma
0
scummos wrote:Hello!

How you do this depends on your build system (and is thus not really related to KDevelop). If you're using cmake, you should be able to solve this by putting something like this into your CMakeLists.txt:
Code: Select all
find_package (Threads)
target_link_libraries (myapp ${CMAKE_THREAD_LIBS_INIT})

(I didn't test that, tough ;P)
If you want to add plain flags, you can also do target_link_libraries(myapp pthread), but that's a bit dirty.

Greetings,
Sven


Thanks!

I tried the first option and it worked great.

Thanks a lot, again.


Regards.


Bookmarks



Who is online

Registered users: bancha, Bing [Bot], Evergrowing, Google [Bot], lockheed, mesutakcan, Sogou [Bot]