linking .c's and .h's in a project file

This is a discussion on linking .c's and .h's in a project file within the C Programming forums, part of the General Programming Boards category; Hi I am creating a program that has three different files. One file contains int main (main.c)and basically just calls ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    linking .c's and .h's in a project file

    Hi I am creating a program that has three different files.

    One file contains int main (main.c)and basically just calls functions from other files.

    Another is a header file (misc.h) which just has declarations of functions.

    The third is another .c file (misc.c) which has all the guts of the functions that were declared in misc.h.


    I have looked at my notes and it says I should do it this way:

    -in main.c
    #include "misc.h"

    in misc.c
    #include "misc.h"

    int misc .h
    #ifndef _misc_h
    #define _misc_h
    ...
    #endif


    I have created a project file (console application) and put everything into one directory.
    I personally dont see how the two .c's are linked , and it doesnt work lol

    (also why cant I insert the .h file into
    Thankyou to anyone who'l help me

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Please describe in what way "it doesn't work" exhibits itself. Does it not compile, does not give you the expected result, crashes, or what? We probably also need to see some example code.

    A project consists of some source files (.c), that produce object code, and they are put together with the linker.

    There are also .h files, which contain information (declarations mainly) that are shared between different source files. These are technically not part of a project [as defined by most IDE's], in that the header files only affect what needs to be compiled when [because if you change a header file, the source that includes that header file will need to be recompiled even if the source itself didn't change]. The build system will known this by itself [except if you write makefiles by hand]. So you do not, in most IDE's, put header files as part of your project definition, since they are, technically, not part of the source code. Yes, the are important to the project, but they are not source that is being compiled and linked to form the executable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by dezz101 View Post
    I have created a project file (console application) and put everything into one directory.
    I personally don't see how the two .c's are linked , and it doesn't work lol
    Should work from first glance. What are you using to link them?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    how does it know that misc.h is a part of the project, as I cant right click on the project window and insert a .h file. Is it good enough that they are in the same directory and for that matter how are .h files included that are not in the same directory (such as stdio.h)???

    I still dont see how the .c files are linked ie im calling functions from main.c, that are declared in misc.h, that are written in misc.c

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    sorry if i asked questions in that last post that you had already answered matsp, it sorta makes a little more sence now that I have seen your post. I will try to create a small project to see how to do it may post sample code if I'm stuck thanks.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    The problem is that you have two "main" functions. The main function is always the entry point and you can only have one. There are three solutions.

    First change the second program to a function. You can then either:

    1. Just append it to the program.
    2. Include the function as you would a header (.h) file. The syntax is a little different however and you need to instruct the compiler where to look for non-standard includes. Let's say you create a directory /foo/includes. You would then place the function in /foo/includes. In you compile command would look like this. "cc -I/foo/includes foo.c". Also in the main program the syntax would be:

    #include "function_name"

    Notice that it is quoted instead of the usual <xxx.h>.

    3. Actually create your own library which can then be referenced during the link phase of your compilation. This is a little more complicated but more useable in the end. What you need to do is compile the function without the link phase and then archive it into a lib file. Then, in you compile command you must tell the compiler to search that archive (the lib file) by using the -l option. If this function is going to be used in many functions this is the way to go.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The builder that is part of your IDE will understand what header files are included by what - for example gcc can give this information with gcc -MM. If the compiler used is not gcc, then it is perfectly possible, and not VERY difficult to figure out which header files are used by which source file. Your source file, after all, contains #include "misc.h".

    The linker, which is program responsible for the production of the executable file will take the object files produced by the compiler, and link the different components together. The compiler, when the compiler finds a function used that doesn't have a definition available in the compile unit (the current source code is the same as a compile unit in most cases [and for this discussion we can assume that source code and compile unit are indeed the same thing]), the compiler produces the code to call it, but does not fill in "where" that function is located. The linker will find those "holes" in the object files that need to be filled in, and match up the "hole" with the function in some other object file [because the compiler also puts a list of those places, and what they should be filled in with]. If it doesn't find the correct data [because you are calling function "func1", but never defined a function "func1" in any source file], the linker will say "undefined symbol func1" or something similar.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21