LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-28-2005, 10:43 PM   #1
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Rep: Reputation: 30
C++ pthread compile errors and segfaults


I am trying to figure out programming with threads and found a very good introduction on the Lawrence Livermore Nation Laboratory's site but can't quite seem to get it running. I copied the code from the first example but can't seem to get it to work right, modified or unmodified. If I try to compile the code as it is on the website, I get this error:
Code:
/home/brandon/thread/src/thread.cpp:32: undefined reference to `pthread_create'
but if I just add #include <iostream> (I was planning on replacing printf()'s with cout's) and don't make any other changes, then the program appears to compile just fine but here is the output when the program is run:
Code:
Creating thread 0
/bin/sh: line 1: 27948 Segmentation fault      ./thread
Press Enter to continue!
Does anyone know why the code wouldn't compile without iostream and why it run once it does compile? If it helps, I am running FC3, GCC 3.4.4, and using KDevelop to code. The code is below.

-Brandon Rich

Code:
/******************************************************************************
* FILE: hello.c
* DESCRIPTION:
*   A "hello world" Pthreads program.  Demonstrates thread creation and
*   termination.
* AUTHOR: Blaise Barney
* LAST REVISED: 04/05/05
******************************************************************************/
#include <pthread.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define NUM_THREADS	5

//using namespace std;


void *PrintHello(void *threadid)
{
	printf("\n%d: Hello World!\n", threadid);
	pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
	pthread_t threads[NUM_THREADS];
	int rc, t;
	for(t=0;t<NUM_THREADS;t++)
	{
		printf("Creating thread %d\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);
		}
	}
	pthread_exit(NULL);
}
 
Old 11-29-2005, 12:17 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Re: C++ pthread compile errors and segfaults

Quote:
Originally posted by spaaarky21
Code:
/home/brandon/thread/src/thread.cpp:32: undefined reference to `pthread_create'
This message is generated during the linker stage of your build process. It indicates that the linker can't find the function.
I don't know the exact command that you use to compile/link, but adding -lpthread at the end should solve the linker problem. This tells the linker to link the pthread library in when building the executable.
In your KDevelop, look for something in the options that refers to libraries and add the pthread library.

I don't understand why '#include <iostream> should make it build; it does not make sense to me. C++ compilers are a lot more sensitive for little mistakes then C compilers. Do you compile with -Wall option to enable all warnings? Again check your KDevelop options.

Your code does not run once, it just starts once ; it executes till the first call of pthread_create and dies with a segmentation fault.

PS:
If you want to compile the single file from the commandline:
Code:
g++ -Wall thread.cpp -o myfirstthread -lpthread
 |    |      |        |      |             |
 |    |      |        |      |             +---- link library pthread
 |    |      |        |      +------------------ output file name
 |    |      |        +------------------------- specify output file name
 |    |      +---------------------------------- input filename
 |    +----------------------------------------- all warnings on
 +---------------------------------------------- build command
 
Old 11-29-2005, 01:57 AM   #3
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Original Poster
Rep: Reputation: 30
Re: Re: C++ pthread compile errors and segfaults

After mucking through some automake files last night, I had no idea that compiling a simple program could be so easy. I've always used CodeWarrior for Mac and Windows development so moving to Linux and GCC is a little tough - makes me appreciate CW's more descript error messages and the way it differentiates compile time errors from link errors.

Anyway, sure enough, after fixing a problem that the -Wall option made apparent, the -lpthread option fixed the segfault error. So how exactly does that work anyway? Is that an option specifically for this one library or can you always just use -l<library_to_link> with no space in between to link a library into an executable?
 
Old 11-29-2005, 02:59 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
yep, for each library add a -l and a name
Code:
..... -lpthread -lm
will add the pthread library and the math library. The biggest problem that I experience is to find the correct library. You might have to search /usr/lib to find the lib. For the pthread, do
Code:
ls /usr/lib |grep pthread
For a program that consists of a few files this will also work quite easy. At the moment that the number of source files grows, you compile all sources every time and that will get time-consuming. You might consider to use the makefile that I posted in http://www.linuxquestions.org/questi...readid=380437; just modify the compiler (cc=)and add/remove libraries (ldflags=) This works as long as you don't split the 'project' over multiple directories.
The makefile will automatically check dependencies (so if you change an include file, it will recompile all sources that have it included). As you might know, make will only recompile if a source is newer than the associated object file which might significantly improve build-time on larger 'projects'.
 
Old 11-29-2005, 08:46 PM   #5
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Original Poster
Rep: Reputation: 30
Thanks for all the info. Is there anywhere online that would have somewhat of a "library reference" that would tell what libraries to link against when using a certain header? I can't even get a program using stdio.h to run without segmentation faults.
 
Old 11-29-2005, 11:46 PM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
For stdio, no additional library is required. There must be something else wrong. You can always post problem code here.
 
Old 11-30-2005, 12:53 AM   #7
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Wim Sturkenboom
For stdio, no additional library is required. There must be something else wrong. You can always post problem code here.
You were right once again. I am using stdio since there is no standard C++ equivalent to popen() but haven't used C-style scan and print functions for probably 5 years by now and had forgot to pass fscanf() the pointer to my int instead of the int itself.

Thanks again for all your help. It's always nice to meet people who are so helpful when things aren't going as expected. The LinuxQuestions.org community is great.
 
  


Reply

Tags
linker, linking, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't compile the kernel - segfaults randomly kleptophobiac Linux - Software 7 07-02-2004 03:13 PM
pthread problems and parse errors Stabby Red Hat 1 01-28-2004 11:56 PM
Make Segfaults Or Spews Random Errors When Compiling 2.6 kernel johnleemk Linux - Software 3 01-04-2004 04:24 PM
Make Segfaults Or Spews Random Errors When Compiling 2.6 kernel johnleemk Linux - General 0 12-28-2003 10:50 AM
compile options for pthread with semaphore megaspaz Programming 6 10-29-2003 06:05 PM


All times are GMT -5. The time now is 07:23 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration