The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk Shell Script error : "Syntax Error : `Split' unexpected Herry UNIX for Dummies Questions & Answers 2 03-17-2008 08:16 AM
error during run: St9bad_alloc - Getting this error while using some conversion progr sathu_pec Shell Programming and Scripting 1 01-20-2008 11:38 PM
I got error like...syntax error on line 1, teletype koti_rama UNIX for Advanced & Expert Users 2 07-07-2007 04:35 PM
error reading sections error at install doelman SUN Solaris 2 02-05-2007 09:21 AM
Error: Internal system error: Unable to initialize standard output file firkus UNIX for Dummies Questions & Answers 2 10-25-2005 12:23 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #8  
Old 07-05-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
Comment out this line:
Code:
extern char *strerror(int errno);
strerror is defined in string.h so your declaration is redundant. I'm guessing it must have been needed on some other system that didn't declare it in string.h, so if you want to be really thorough, you could do something like:

Code:
// Near top util.h
#include <string.h>

// Lower down
#ifndef strerror
extern char *strerror(int errno);
#endif/*strerror*/
Including string.h more than once shouldn't hurt because it has start and end code like this:

Code:
// Start of file
#ifndef __STRING_H__
#define __STRING_H__

// file contents
...

#endif/*__STRING_H__*/
I add these to all my .h files just to be safe.
Reply With Quote
Forum Sponsor
  #9  
Old 07-05-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
strerror() is declared in string.h according to ISO
It's strange that this code worked earlier
Reply With Quote
  #10  
Old 07-06-2006
Registered User
 

Join Date: Jun 2006
Location: Malaysia
Posts: 14
Dear Corona

If I comment out this what I will get in error

couliba@linux:~/bin/GaSource> make find_rules
cc find_rules.c -o find_rules
/tmp/ccsfOXjS.o(.text+0x72): In function `main':
find_rules.c: undefined reference to `seed'
/tmp/ccsfOXjS.o(.text+0x18e):find_rules.c: undefined reference to `print_help_message'
/tmp/ccsfOXjS.o(.text+0x23d):find_rules.c: undefined reference to `read_cline'
/tmp/ccsfOXjS.o(.text+0x679):find_rules.c: undefined reference to `define_type'
/tmp/ccsfOXjS.o(.text+0x68c):find_rules.c: undefined reference to `define_type'
/tmp/ccsfOXjS.o(.text+0x69f):find_rules.c: undefined reference to `define_type'
/tmp/ccsfOXjS.o(.text+0x6e5):find_rules.c: undefined reference to `define_function'
/tmp/ccsfOXjS.o(.text+0x702):find_rules.c: undefined reference to `define_tree'
/tmp/ccsfOXjS.o(.text+0x789):find_rules.c: undefined reference to `init_data'
/tmp/ccsfOXjS.o(.text+0x821):find_rules.c: undefined reference to `read_data'
/tmp/ccsfOXjS.o(.text+0x850):find_rules.c: undefined reference to `normalize_price'
/tmp/ccsfOXjS.o(.text+0xe30):find_rules.c: undefined reference to `compounded_return'
/tmp/ccsfOXjS.o(.text+0xe61):find_rules.c: undefined reference to `compounded_return'
/tmp/ccsfOXjS.o(.text+0xf30):find_rules.c: undefined reference to `new_tree'
/tmp/ccsfOXjS.o(.text+0x10fa):find_rules.c: undefined reference to `create_population'
/tmp/ccsfOXjS.o(.text+0x1151):find_rules.c: undefined reference to `create_offspring'
/tmp/ccsfOXjS.o(.text+0x11da):find_rules.c: undefined reference to `eval_genome'
/tmp/ccsfOXjS.o(.text+0x1210):find_rules.c: undefined reference to `compute_return'
/tmp/ccsfOXjS.o(.text+0x123f):find_rules.c: undefined reference to `compounded_return'
/tmp/ccsfOXjS.o(.text+0x12a1):find_rules.c: undefined reference to `compute_statistics'
/tmp/ccsfOXjS.o(.text+0x12b8):find_rules.c: undefined reference to `cluster_population'
/tmp/ccsfOXjS.o(.text+0x14cf):find_rules.c: undefined reference to `eval_genome'
/tmp/ccsfOXjS.o(.text+0x1505):find_rules.c: undefined reference to `compute_return'
/tmp/ccsfOXjS.o(.text+0x1534):find_rules.c: undefined reference to `compounded_return'
/tmp/ccsfOXjS.o(.text+0x17ce):find_rules.c: undefined reference to `copy_genome'
/tmp/ccsfOXjS.o(.text+0x1b79):find_rules.c: undefined reference to `save_genome'
/tmp/ccsfOXjS.o(.text+0x1c24):find_rules.c: undefined reference to `eval_genome'
/tmp/ccsfOXjS.o(.text+0x1c5e):find_rules.c: undefined reference to `compute_return'
/tmp/ccsfOXjS.o(.text+0x1c91):find_rules.c: undefined reference to `compounded_return'
/tmp/ccsfOXjS.o(.text+0x1d9f):find_rules.c: undefined reference to `dispose_population'
/tmp/ccsfOXjS.o(.text+0x1de3):find_rules.c: undefined reference to `descending_order'
/tmp/ccsfOXjS.o(.text+0x1dfc):find_rules.c: undefined reference to `order_by_key'
/tmp/ccsfOXjS.o(.data+0x50): undefined reference to `print_boolean'
/tmp/ccsfOXjS.o(.data+0x54): undefined reference to `eval_if_then'
/tmp/ccsfOXjS.o(.data+0xa0): undefined reference to `print_boolean'
/tmp/ccsfOXjS.o(.data+0xa4): undefined reference to `eval_if_then_else'
/tmp/ccsfOXjS.o(.data+0xf0): undefined reference to `print_boolean'
/tmp/ccsfOXjS.o(.data+0xf4): undefined reference to `eval_and'
/tmp/ccsfOXjS.o(.data+0x140): undefined reference to `print_boolean'
/tmp/ccsfOXjS.o(.data+0x144): undefined reference to `eval_or'
/tmp/ccsfOXjS.o(.data+0x190): undefined reference to `print_boolean'
/tmp/ccsfOXjS.o(.data+0x194): undefined reference to `eval_not'
/tmp/ccsfOXjS.o(.data+0x1e0): undefined reference to `print_boolean'
/tmp/ccsfOXjS.o(.data+0x1e4): undefined reference to `eval_greater'
/tmp/ccsfOXjS.o(.data+0x230): undefined reference to `print_boolean'
/tmp/ccsfOXjS.o(.data+0x234): undefined reference to `eval_less'
/tmp/ccsfOXjS.o(.data+0x278): undefined reference to `init_boolean'
/tmp/ccsfOXjS.o(.data+0x27c): undefined reference to `read_boolean'
/tmp/ccsfOXjS.o(.data+0x280): undefined reference to `print_boolean'
/tmp/ccsfOXjS.o(.data+0x2d4): undefined reference to `eval_flip'
/tmp/ccsfOXjS.o(.data+0x320): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x324): undefined reference to `eval_plus'
/tmp/ccsfOXjS.o(.data+0x370): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x374): undefined reference to `eval_minus'
/tmp/ccsfOXjS.o(.data+0x3c0): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x3c4): undefined reference to `eval_multiply'
/tmp/ccsfOXjS.o(.data+0x410): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x414): undefined reference to `eval_divide'
/tmp/ccsfOXjS.o(.data+0x460): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x464): undefined reference to `eval_negation'
/tmp/ccsfOXjS.o(.data+0x4b0): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x4b4): undefined reference to `eval_norm'
/tmp/ccsfOXjS.o(.data+0x500): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x504): undefined reference to `eval_average'
/tmp/ccsfOXjS.o(.data+0x550): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x554): undefined reference to `eval_maximum'
/tmp/ccsfOXjS.o(.data+0x5a0): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x5a4): undefined reference to `eval_minimum'
/tmp/ccsfOXjS.o(.data+0x5f0): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x5f4): undefined reference to `eval_slope'
/tmp/ccsfOXjS.o(.data+0x640): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x644): undefined reference to `eval_volatility'
/tmp/ccsfOXjS.o(.data+0x690): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x694): undefined reference to `eval_lag'
/tmp/ccsfOXjS.o(.data+0x6e0): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x6e4): undefined reference to `eval_data'
/tmp/ccsfOXjS.o(.data+0x728): undefined reference to `init_real'
/tmp/ccsfOXjS.o(.data+0x72c): undefined reference to `read_real'
/tmp/ccsfOXjS.o(.data+0x730): undefined reference to `print_real'
/tmp/ccsfOXjS.o(.data+0x784): undefined reference to `eval_price'
/tmp/ccsfOXjS.o(.data+0x7d4): undefined reference to `eval_compounded'
collect2: ld returned 1 exit status
make: *** [find_rules] Error 1


and If add the code as the below

/*------------------------------------------------------------------------------

File: util.h
Date: January 26, 1992

Copyright (c) Risto Karjalainen. All rights reserved.

Utility functions of general use.

------------------------------------------------------------------------------*/
#inlcude <string.h>

#define max(x,y) ((x) > (y) ? (x) : (y))
#define min(x,y) ((x) < (y) ? (x) : (y))
#ifndef strerror
extern char *strerror(int errno);
#endif

/*------------------------------------------------------------------------------
'get_boolean' prints '*prompt' and reads a character from stdin. 'answers'
contains the valid character set. If the entered character equals 'affirm',
'def' is returned.
------------------------------------------------------------------------------*/

extern int get_boolean(char *prompt, char *answers, int def, char affirm);

/*------------------------------------------------------------------------------
'get_int' prints '*prompt' and reads an integer from stdin. The entered
integer must be in the range 'min' .. 'max'. If none is entered, 'def'
is returned, instead.
*/

I will receive this error

ouliba@linux:~/bin/GaSource> make find_rules
cc find_rules.c -o find_rules
In file included from find_rules.c:19:
util.h:17: error: conflicting types for `strerror'
/usr/include/string.h:256: error: previous declaration of `strerror'
find_rules.c: In function `main':
find_rules.c:203: warning: passing arg 1 of `strerror' makes pointer from integer without a cast
find_rules.c:248: warning: passing arg 1 of `strerror' makes pointer from integer without a cast
find_rules.c:315: warning: passing arg 1 of `strerror' makes pointer from integer without a cast
make: *** [find_rules] Error 1
couliba@linux:~/bin/GaSource>


Any more advise, I appreciate so much



Quote:
Originally Posted by Corona688
Comment out this line:
Code:
extern char *strerror(int errno);
strerror is defined in string.h so your declaration is redundant. I'm guessing it must have been needed on some other system that didn't declare it in string.h, so if you want to be really thorough, you could do something like:

Code:
// Near top util.h
#include <string.h>

// Lower down
#ifndef strerror
extern char *strerror(int errno);
#endif/*strerror*/
Including string.h more than once shouldn't hurt because it has start and end code like this:

Code:
// Start of file
#ifndef __STRING_H__
#define __STRING_H__

// file contents
...

#endif/*__STRING_H__*/
I add these to all my .h files just to be safe.
Reply With Quote
  #11  
Old 07-07-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
That error is what Corona is trying to help you with - you have strerror declared in util.h. Put the #ifndef and #define statement he gave you around the place where you encounter the error in util.h.

To find where strerror is doubly defined try this:
Code:
gcc -E find_rules.c | grep 'strerror'
You will see something like "extern char *strerror" appearing twice. It should show up only once.
Reply With Quote
  #12  
Old 07-07-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
Those errors are exactly what it says they are. It cannot find those functions. You probably need to compile another file to get them. Not all C files are standalone. The general sequence is:

Code:
cc -c file1.c -o file1.o
cc -c file2.c -o file2.o
...
cc -c file5.c -o file5.o
cc file1.o file2.o file3.o file4.o file5.o -o program
In a makefile you might have something like:
Code:
OBJS=file1.o file2.o file3.o file4.o file5.o

program:$(OBJS)
        $(CC) $(OBJS) $(LDFLAGS) -o $@
It should already understand how to convert .c to .o, so you just tell it how to convert those .o's to one executable. LDFLAGS is an optional variable that might comtain library flags.

And please, for the love of yog, put your code in code-tags!!

Last edited by Corona688; 07-07-2006 at 07:26 AM.
Reply With Quote
  #13  
Old 07-08-2006
Registered User
 

Join Date: Jun 2006
Location: Malaysia
Posts: 14
Quote:
Originally Posted by Corona688
Those errors are exactly what it says they are. It cannot find those functions. You probably need to compile another file to get them. Not all C files are standalone. The general sequence is:

Code:
cc -c file1.c -o file1.o
cc -c file2.c -o file2.o
...
cc -c file5.c -o file5.o
cc file1.o file2.o file3.o file4.o file5.o -o program
In a makefile you might have something like:
Code:
OBJS=file1.o file2.o file3.o file4.o file5.o

program:$(OBJS)
        $(CC) $(OBJS) $(LDFLAGS) -o $@
It should already understand how to convert .c to .o, so you just tell it how to convert those .o's to one executable. LDFLAGS is an optional variable that might comtain library flags.

And please, for the love of yog, put your code in code-tags!!
Your advice may be a solution to my problem, but I am not sure if I understand what you want me to do. Can you please put in away I can understand.

As you said. it can not find those function. that is right but how do I make it to find those function.

Thank you very much
Reply With Quote
  #14  
Old 07-10-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
This might best be shown by example.

In a directory by itself, create this file:
Code:
// main.c
#include <stdio.h>

int main(int argc, char *argv[])
{
  int n;
  fprintf(stderr,"main(%d,[",argc);
  for(n=0; n<argc; n++)
    fprintf(stderr," '%s'",argv[n]);
  fprintf(stderr," ]);\n");
  return(0);
}
Then compile and run it like this:
Code:
# gcc main.c -o main
# ./main
main(1,[ './main' ]);
#
That works, but what you might not realize is that gcc is actually doing two steps, not one -- compilation, then linking. The compilation stage converts C code into assembly language, the linking stage ties everything together into an executable. Let's break it into two steps:
Code:
# gcc -c main.c -o main.o
# gcc main.o -o main
# ./main
main(1,[ './main' ]);
#
That's what gcc is ACTUALLY doing... converts the C file into an object file(.o), then links the object file into an executable.

Now let's try and use a function from another .c file. First, create this file:
Code:
// library.c
#include <stdio.h>

int library_function(int c)
{
  fprintf(stderr,"library_function(%d)\n",c);
  return(c-3);
}
Then, modify main.c to use it like this:
Code:
#include <stdio.h>

extern int library_function(int c);

int main(int argc, char *argv[])
{
  int n;
  fprintf(stderr,"main(%d,[",argc);
  for(n=0; n<argc; n++)
    fprintf(stderr," '%s'",argv[n]);
  fprintf(stderr," ]);\n");

  printf("library_function(%d) returns %d\n",32,library_function(32));

  return(0);
}
Finally, let's see what happens when we try and compile it, then link it:
Code:
# gcc -c main.c -o main.o
# gcc main.o -o main
main.o: In function `main':
main.c:(.text+0x86): undefined reference to `library_function'
collect2: ld returned 1 exit status
#
It doesn't work. It compiles it fine since there's no syntax errors in the program, but the function it wants is not in main.o! To get that function, we'll need to give it library.o too.
Code:
# gcc -c main.c -o main.o
# gcc -c library.c -o library.o
# gcc main.o library.o -o main
# ./main
main(1,[ './main' ]);
library_function(32)
library_function(32) returns 29
#
It works!

Now, if you want your makefile to do that, you need to tell it that the output file needs main.o and library.o. Here's a simple makefile:
Code:
main:main.o library.o
        $(CC) main.o library.o $(LDFLAGS) -o $@
clean:
        rm -f main main.o library.o
With that makefile in the main directory, let's try it:
Code:
# make clean
rm -f main main.o library.o
# make main
cc    -c -o main.o main.c
cc    -c -o library.o library.c
cc main.o library.o  -o main
# ./main
main(1,[ './main' ]);
library_function(32)
library_function(32) returns 29
#
It works!

So there you go. You'll need to find the .c file that these missing functions reside in, convert it to an object, then link it into your executable.

Last edited by Corona688; 07-11-2006 at 07:25 AM. Reason: mistake in examples
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0