Solved

C++ multithreading code does not compile

Posted on 2013-01-16
4
407 Views
Last Modified: 2013-01-16
Hello experts!

I am using ubuntu and geanie.
I try to compile following code:


/******************************************************************************
* FILE: hello.c
* DESCRIPTION:
*   A "hello world" Pthreads program.  Demonstrates thread creation and
*   termination.
* AUTHOR: Blaise Barney
* LAST REVISED: 08/09/11
******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS      5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0;t<NUM_THREADS;t++){
     printf("In main: creating thread %ld\n", t);
     rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
     if (rc){
       printf("ERROR; return code from pthread_create() is %d\n", rc);
       exit(-1);
       }
     }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}


I get error messages:


g++ -Wall -o "main" "main.cxx" (in directory: /home/jacek/Documents/cpp/wlasne/MultiThreading/4)
/tmp/ccvE3G41.o: In function `main':
main.cxx:(.text+0x85): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
Compilation failed.

What do I do wrong?

Thank you

panJames
0
Comment
Question by:panJames
  • 2
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 38783034
Hi panJames,

I guess you need to link 'pthread' library into your program. Try adding -lpthread to the command line ...

Hope that helps,

ZOPPO
0
 

Author Comment

by:panJames
ID: 38783126
g++ -Wall -o -lpthread "main" "main.cxx" (in directory: /home/jacek/Documents/cpp/wlasne/MultiThreading/4)
g++: error: main: No such file or directory
Compilation failed.
0
 

Author Comment

by:panJames
ID: 38783131
g++ -Wall -o -lpthread "%e" "%f"
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 38783141
Hi,

you should not add this directly after -o. The argument after -o is the name of the resulting executable.

ZOPPO
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Fine Tune your automatic Updates for Ubuntu / Debian
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now