Solved

C++ multithreading code does not compile

Posted on 2013-01-16
4
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

726 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