Thread: header files and multiple files

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    56

    header files and multiple files

    Hi

    I'm tring to learn how to write a program with multiple files.
    I have a working program composed by a file main.c with many functions inside. I've tried to put one function outside.
    I mean that now i have:

    1) main.c ( with the #include " function1.h" and where i call the function1),
    2) funtion1.c ( with the #include " function1.h", and where I write what the function1 do),
    3)function1.h (where all i do is just to declare the function1)

    but now the program doesn't not compile and i get the linker error:

    Code:
    /main.c:32: undefined reference to `function1'
    collect2: ld returned 1 exit status
    Anyone can help?

    Thanks

    D.

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    looks like you're working with visualC.
    Add the .c file to your project.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    Sorry I'm not sure to understand.
    Do you mean to add function1.c in the same directory of main.c? it already is.

    thx

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    No, you need to tell your linker that you are linking more than one source file together. Just having a source file wherever it may be does not mean the linker understands that it needs to be combined with any others to produce a complete executable. Typically, with IDEs you create a project and add all your source files to that project, then when the compiler/linker is called it knows to compile and link everything together.

    What compiler/IDE are you using?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    yes you are right.

    I use gcc.

    I've done it !, just telling it to gcc in the line command.

    Thx
    D.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Dedalus View Post
    I use gcc.
    Okay you have something backward then, consider:

    1) main.c ( with the #include " function1.h" and where i call the function1),
    2) funtion1.c ( with the #include " function1.h", and where I write what the function1 do),
    3)function1.h (where all i do is just to declare the function1)
    Hmmm...looks like niether main.c nor function1.h include function1.c


    Persuming you just want to go "gcc main.c", what you should have is:

    1) main.c with #include "function1.h"
    2) function1.h with #include "function.c"
    3) function1.c doesn't need to include anything.

    But I would just combine function.c into function1.h anyway. You can put more than just prototypes into an .h file. Then there will be just one include file. Simple enough?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple header files , cout undelcared probem
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2005, 10:15 PM
  2. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  3. Replies: 4
    Last Post: 06-18-2005, 02:26 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Conflicting Header Files
    By X PaYnE X in forum Windows Programming
    Replies: 17
    Last Post: 01-08-2004, 11:28 AM