Thread: problem creating header files

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    99

    problem creating header files

    I am trying to write a header file. The reason I do this is because I want to test a library of functions, this file should itself not contain a main(). (Just to give you an idea of what I am trying to achieve).

    So i have three files:
    (1) messaging2.c (a set of useful functions)
    (2) messaging2.h
    (3) testMessaging.c (which includes #include "messaging2.h" and contains some tests)

    Function signatures are included. However when I try to complie (3) i get:

    testMessaging2. .text: undefined reference to '_checkMessage'
    testMessaging2. .text: undefined reference to '_createMessage'

    However, both signatures are in the header file (and in the original program file off course).

    I know this is hard to answer without code, but before pasting a lot of code I'd thought I'd try to see if there is a very common cause for this other wise I will post the code.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You said you were creating a library. Did you tell your compiler that (by, for instance, using the -c switch)?

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    I guess I used the wrong term then, what I meant to say was that I am creating a file with a few utility functions but without a main(), I called this a library but judging from what you're asking it is probably the wrong word. It is simply a file with functions which I will use in other programs.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    So, you just want to make a multiple source file project? We have an example of that here. It is in C++ but you should be able to get the idea.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by django View Post
    I guess I used the wrong term then, what I meant to say was that I am creating a file with a few utility functions but without a main(), I called this a library but judging from what you're asking it is probably the wrong word. It is simply a file with functions which I will use in other programs.
    No, it is not the wrong word, altho you are using it slightly ambiguously because it is not clear whether you want an actual library -- a binary executable that can be linked to by another binary executable -- or what is called an object file, which is the same thing, but rather than being linked at runtime, the object file is compiled together with other object or source files to create a single executable.

    How all that is done depends on your compiler. So, using your example and gcc, you might do this:

    gcc -c messaging2.c

    By default, this will create an object file, messaging2.o. To use the functions from messaging2.h in testMessaging.c, after compiling the object you would use:

    gcc messaging2.o testMessaging.c

    And by default you will have an executable, a.out, which contains everything required, ie, you can now erase messaging2.o (or save it for re-use).

    If you compiled messaging2.c as a library proper, you would not compile it into the executable created from testMessaging.c, you would have it linked something like this:

    gcc testMessaging.c -lmessaging2

    And messaging2 would have to exist whenever that executable is run. The advantage of this is various programs can use the library but it only need by loaded into memory once, and you will not need to recompile everything if you upgrade it. A disadvantage is that your "upgrade" can then break existing programs that link to the library.

    Altho the code for a library is identical to the code for an object file (.o), you should learn to work with object files first as compiling them is simpler and you don't really need an independent library, if it is just some code that you want to use various places.
    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

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    The background I currently have is wat Kernighen and Ritchie tell about it (which is not much) and a short article I found. I guess that gives me a general idea of what it should contain. I saw the article here, but it is about C++ so I decided to find something myself on headers in c.

    I am learning C on my windows pc using an LCC compiler. I was not aware of what MK27 is telling about additional compiler directives and working with object files. I will study that first a bit. To be quite frank, I am aware of the existence of object files, but have no clear idea on what can be done with them. If you would have a good starting read on it, I would be grateful.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    A good general overview can be found: compiling and linking - CBoard. It will give you the gist on what is going on.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by AndrewHunter View Post
    A good general overview can be found: compiling and linking - CBoard. It will give you the gist on what is going on.
    That's good.

    I should point out that it might be slightly confusing after what I wrote. It's more technically precise; I was using compiling in what's referred to there as "the colloquial sense" and linking to refer specifically to linking at runtime. However, as that article explains, linking is necessary to the creation of an executable. Generally your compiler runs the linker for you, so that is hidden.

    Quote Originally Posted by django View Post
    To be quite frank, I am aware of the existence of object files, but have no clear idea on what can be done with them.
    I think everyone will agree that they are exactly what you want to use here. I haven't used LCC but I just looked at a manual page online and it uses the same -c switch, so try:

    lcc -c messaging2.c

    and see what happens. You should have a (non-executable) "messaging2.o" created and then be able to

    lcc messaging2.o testMessaging.c

    If everything is as you said it was in your first post, this should work. If not, post the errors here.

    Libraries are the executable form of object files. Generally they do not do anything useful if you try to execute them by themselves (since there's probably no main()), but runtime linkage means the executable functions within them can be used by another program.
    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

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by MK27 View Post
    I should point out that it might be slightly confusing after what I wrote. It's more technically precise; I was using compiling in what's referred to there as "the colloquial sense" and linking to refer specifically to linking at runtime. However, as that article explains, linking is necessary to the creation of an executable. Generally your compiler runs the linker for you, so that is hidden.
    Yes, and I do agree. I was simply providing a source to fill in the level of knowledge gaps for the OP. If he understands what is actually happening he will be better off in both asking his questions about and understanding his options for, a multiple source file program.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    I read up on the object files and a bit more on the compiling and linking process. To understand things better, I am using a commandline .bat file to explicitly compile and link. I will study the command line options. My current understanding is that I have two options:
    - link the object files into one executable
    - keep the executable (in this case testMessaging2.exe) separate from messaging.o but this will require this file in order to be able to run testMessaging2.exe

    One question though: In the .c file, there is a struct like this:
    Code:
    struct order {
    	int ID;
    	char dir[3];
    	int actual_size;
    	int filled_size;
    	float price;
    	char instr[5];
    };
    From what I read here and there, I should declare this in the .h file as
    Code:
    struct order;
    Is that correct?
    Thanks for the help above, going to give it a try.
    Last edited by django; 08-10-2011 at 02:12 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you intend to use the struct in various .c files, then you need the full definition of the struct in the .h file, not in the .c files.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Ok, great that did the trick. I now succeed in building one executable.

    What I did seems fairly primitive, these are the commands i used:

    lcc ..\source\messaging2.c
    lcc ..\source\testMessaging2.c
    lcclnk testMessaging2.obj messaging2.obj

    I use no compiler options, just compile both files and link the resulting object files together into one executable file (by default this linker gets the first filename as the name of the executable.

    I will now figure out how to create a library that remains separate from the final executable (and therefore needs to be present). B.t.w., detailed stuff like that about how to handle a struct when creating a .h file, where should I look for that?
    Last edited by django; 08-10-2011 at 04:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with header files
    By Farnaz in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2011, 12:11 PM
  2. Creating header files
    By histevenk in forum C++ Programming
    Replies: 14
    Last Post: 10-17-2007, 08:02 PM
  3. Creating Header Files
    By Neo1 in forum C++ Programming
    Replies: 30
    Last Post: 09-30-2006, 03:26 PM
  4. creating header files
    By GamingMarvel in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2005, 06:57 AM
  5. creating header files and libs
    By iain in forum C Programming
    Replies: 1
    Last Post: 11-10-2002, 03:31 PM