Thread: need assistance with multiple files

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    Post need assistance with multiple files

    ok i got two files, one has functions in it the other uses them, pretty simple right

    in one file i have this
    Code:
    static void xtoa(unsigned long val, char *buf, unsigned int radix, int negative)
    {...}
    char *itoa(int val, char *buf, int radix)
    {...}
    In the other i had
    Code:
    extern static void xtoa(unsigned long val, char *buf, unsigned int radix, int negative);
    extern char* itoa(int, char*, int);
    which gave me an error saying multiple storage classes, which i solved by removing the extern infront of "static void xtoa...", but now i get the error
    Code:
    Server.o(.text+0x2178):Server.cpp: undefined reference to `itoa(int, char*, int)'
    this is the error i cant seem to solve, i have the definitions the same in both files, so why cant it find it?
    (and yes i do link them together when i compile the project)

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    When 'static' is applied to a function, it means the function is defined inside the file. extern means the function is defined outside the scope it is declared in.

    Try this:

    1) Put this function in your first file:

    Code:
    #include <iostream>
    using namespace std;
    ...
    ...
    void test()
    {
         cout<<"hello world"<<endl;
    }
    Code:
    2) Put this in your second file:
    
    void test();
    
    int main()
    {
         test();
        
         .....
    Last edited by 7stud; 03-20-2006 at 02:26 AM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    if say i have a function
    static int test()
    in another file would i proto type it
    extern static int test()
    extern int test()
    or
    static int test()
    ?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Reread my earlier post, I added something.

    2) Why are you messing around with the 'static' and 'extern' keywords?

    3)
    in another file would i proto type it
    extern static int test()
    Since static means DO NOT look outside the current file for the function definition, and extern means GO LOOK OUTSIDE the current scope for the definition, what could you possibly mean by declaring a function as 'extern static'?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > which gave me an error saying multiple storage classes, which i solved by removing the extern
    You should have removed the static, if you're calling a function from multiple source files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You should have removed the static, if you're calling a function from multiple source files.
    ...and since functions have external linkage by default, you can remove the extern too, right?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    :P i just realized i dont need to proto type the static function anywere ...

    but that only solves one problem... the main one is still left
    Code:
    Server.o(.text+0x2144): In function `getData(int)':
    Server.cpp: undefined reference to `itoa(int, char*, int)'
    i have tried compiling with and without extern in front of the prototype with no success, so i think i am missing something... here is the whole compiler output in case it helps

    Code:
    $ make -f makefile.server
    gcc -c xtoa.c -o xtoa.o
    g++ -c Server.cpp -o Server.o     -L/usr/local/lib
    g++ -c character.cpp -o character.o     -L/usr/local/lib
    g++ xtoa.o Server.o character.o  -o Server -L/usr/local/lib --export-dynamic -lalleg-4.2.0 -lalleg_unsharable -lNL -lpthreadServer.o(.text+0x2144): In function `getData(int)':
    Server.cpp: undefined reference to `itoa(int, char*, int)'
    Server.o(.text+0x2178):Server.cpp: undefined reference to `itoa(int, char*, int)'
    Server.o(.text+0x21ac):Server.cpp: undefined reference to `itoa(int, char*, int)'
    Server.o(.text+0x21e0):Server.cpp: undefined reference to `itoa(int, char*, int)'
    Server.o(.text+0x2214):Server.cpp: undefined reference to `itoa(int, char*, int)'
    collect2: ld returned 1 exit status
    make: *** [Server] Error 1

    sorry if the answer is there and i just dont see it.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well it can't find itoa()

    Which means either you haven't written it, haven't compiled it, got the prototype wrong (since you're calling it from C++), mis-spelt it somewhere, or not passing the .o file it's in to the linker.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  2. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  3. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  4. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM
  5. Multiple files using RHIDE
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-05-2001, 06:39 PM