By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

syntax error, unexpected token ... what's causing this???

100+
P: 175
I'm using SSH Secure Shell to connect to a Linux machine ... when I try to run the compiled code I have, I keep getting these errors ...

./p2.c: line 5: syntax error near unexpected token `('
./p2.c: line 5: `void *printMessage( void *thread_id );'

here are the first few lines of the code ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5.  
  6. void *printMessage( void *thread_id );
  7.  
  8. int main ( int argc, char *argv[] )
  9. {
  10. ...
  11.  
  12.  
I compiled this code using "gcc -pthread p2.c"
then, tried to run it using "./p2.c"
but get that error

the program deals with threads, but I don't think that has anything to do with it. I'm don't have much experience with Linux, and have no clue why I keep getting this error. Any help would be appreciated.
Mar 21 '08 #1
Share this Question
Share on Google+
4 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
I'm using SSH Secure Shell to connect to a Linux machine ... when I try to run the compiled code I have, I keep getting these errors ...

./p2.c: line 5: syntax error near unexpected token `('
./p2.c: line 5: `void *printMessage( void *thread_id );'

here are the first few lines of the code ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5.  
  6. void *printMessage( void *thread_id );
  7.  
  8. int main ( int argc, char *argv[] )
  9. {
  10. ...
  11.  
  12.  
I compiled this code using "gcc -pthread p2.c"
then, tried to run it using "./p2.c"
but get that error

the program deals with threads, but I don't think that has anything to do with it. I'm don't have much experience with Linux, and have no clue why I keep getting this error. Any help would be appreciated.
This is not a Linux issue, this is a C issue. Look at your function parameters. You are attempting to pass a void *thread_id . A pointer to a void. Since void is used to specify no parameters it's getting confused when you try to send it a thread_id.

So the question is - what is the type of the thread_id. When you declare it in your main what type is it?
Mar 22 '08 #2

100+
P: 175
I don't think it has to do with the type. I tried some different things out ...

I kept the same header files, made a simple function that takes an INT as its only argument and returns an int ... and in main, all I did was pass an int and get it's value back. I got the EXACT SAME ERROR.

So, I ran the same code again, but took out the <pthread.h> header file, and it compiled fine.

This is why I still believe it may be a Linux issue. I may be compiling it wrong, I don't know. I usually use "gcc -o programName programName.c", but I think I need to use "gcc -pthread programName2.c" to compile a program using pthreads.

I'm still lost, and I really do appreciate the help.
Mar 22 '08 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Dang, sorry about that. I think that would have been an issue anyway, but there is another issue. If you look at the error - the problem isn't with the argument, it's with the open parentheses. Or, what's right before the parentheses.


./p2.c: line 5: syntax error near unexpected token `('
./p2.c: line 5: `void *printMessage( void *thread_id );'

Expand|Select|Wrap|Line Numbers
  1. void *printMessage( void *thread_id );
  2.  
Why are you using a pointer for printMessage()? Try removing the * and see what that does.

PS - you do have the definition of this somewhere, right?
Mar 22 '08 #4

100+
P: 175
hey, thank you for the responses. Yes I do have the function defined elsewhere :) I actually never write code like that, but I separated the declaration from the definition trying to pinpoint the problem.

Anyway, I just figured out the problem. It was the way I was compiling. I had to use "gcc -o p2 p2.c -lpthread" to get it to compile right. I had used a ton of combinations of that, but never exactly that. Works perfectly now.

By the way, I had that function as a function pointer because where it's taken in as an argument in "pthread_create", it's looking for a pointer to a function. So, anyway, the code itself was okay.

Again, thanks for the replies, I appreciate the help.
Mar 22 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.