Find the answer to your Linux question:
Results 1 to 7 of 7
hi i am a new programer in c i have write a simple app and i have a problem withe combiling my program include one header file and 2 c ...
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    1

    (.text+0x7): undefined reference to

    hi
    i am a new programer in c
    i have write a simple app and i have a problem withe combiling
    my program include one header file and 2 c

    fun.h

    #include<stdio.h>
    void f();
    /******EOF*******/
    fun.c

    #include"fun.h"
    void f(){
    printf("HI\n");
    }
    /*****EOF********/
    and tha main is

    #include"fun.h"
    int main(int argc,char* argv[]){
    f();
    }
    /*****EOF********/

    and when i trying to compile the program i receive this message
    /tmp/ccwzuzqh.o: In function `main':
    main.c.text+0x7): undefined reference to `f'
    collect2: ld returned 1 exit status

    is there any one can help me ?

  2. #2
    Just Joined!
    Join Date
    Apr 2011
    Posts
    19

    Easy I think

    It is not the compiler that is complaining, it is the linker, the thing that prepares the object files into a executable. Try
    cc main.c fun,c
    I suspect you did
    cc main.c

  3. #3
    Just Joined!
    Join Date
    Feb 2011
    Posts
    83
    Maybe the C compiler assumes f() as an internal C routine and calls a modified version of the name. Rewrite the code by defining f() as an external function and test it with the gcc (GNU compiler collection).

  4. #4
    Just Joined!
    Join Date
    Nov 2008
    Posts
    29

    External declarations

    Hello,

    fun.h should declare
    extern void f(void);

    to be fully compliant with ANSI C.

    The keyword extern indicates that this now is a declaration. Across all compilation units in your program, there can thus only be one definition, but this f.h can now be included everywhere without actually defining the function f().

    Regards,
    Guus

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You need to declare and define f() as "void f(void)", and then you need to make sure you link fun.o with main.o when you compile and link them to make your executable.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Just Joined!
    Join Date
    Feb 2011
    Posts
    83
    IMHO Guru Lewis Junior is right: in order to become a great programmer (some day) you should always read at first 'Das Original' (in this case the ISO Standard of C++, ANSI).

    http://www.open-std.org/jtc1/sc22/wg...2010/n3092.pdf
    There is a new version of it (with insignificant amendments).

    However, if you compare this standard with the Handbook of C++ (that is adorned with the Pirate Logo) you will see a lot of differences.


    To start with something, read first the standards of the developers.

  7. #7
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    21
    hi hassuna,
    I found this (dont remember where) about the structure you should follow in your C projects. Hope it helps

    C PROGRAM STRUCTURE

    // module.h

    #ifndef _MODULE_H // To avoid multiple inclusion
    #define _MODULE_H

    #include “another_include.h” // other includes, if necesary.

    #define DEFINITION // Defines

    typedef long int INT32; // typedefs

    extern int global_variable;
    extern void global_function(int x);

    #endif

    // module.c

    #include <include1.h> // as many includes as we need
    #include “include2.h”

    #include “module.h” // each .c has to incluye its .h

    int global_variable = 1234; // global variable defined as
    // extern in the .h

    static char local_variable; // local Variable only visible
    // from module.c

    void global_function (int x){ // global_function implementation
    . . .
    }

    static int local_function(char c){ // funcion_local Implementation
    . . .
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...