Solved

C++ multithreading code does not compile

Posted on 2013-01-16
4
412 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CLI command keep running after close 7 69
Check for Linux process in script 7 85
sed/awk/tail: how to read 3'de last line 4 64
RPM creation 6 44
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

734 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