Solved

C++ multithreading code does not compile

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ubuntu not booting - How get past GRUB? 3 57
High Bandwidth Usage 6 75
Android development question 2 53
CLI command keep running after close 7 36
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…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

809 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