Thread: Header File Headache

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Header File Headache

    I'm having a terrible time with some user-written header files. I have compiled each .c file successfully with no errors or warnings. But the function calls from main.c give a undefined symbol error. I'm clueless where to go from here. I'm using Turbo C++ 3.0 (ancient I know!). What am I missing here?

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    We can't guess what you have. Show us how you are using your header files and where you are including them. No need to post all of the code, just the relevant lines.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Blah... Sorry. Hit post before I copied my code..

    Here is help_fun.h
    Code:
    #ifndef __HELP_FUN_H
    #define __HELP_FUN_H
    
    #include <string.h>                    // String Functions
    
    char * String_Concat (const char *s1, const char *s2); // Concatenates two strings and returns the result.  Both s1 and s2 are unchanged.
    
    #endif
    Here is help_fun.c
    Code:
    #include <string.h>                    // String Functions
    #include "help_fun.h"
    
    /* String Concatenation Function
       Version 1.0
       February 10, 2009
    
       Return Values : A pointer to a C-Style String
       Parameters    : Two pointers to C-Style Strings
       Description   : Concatenates two C-Style strings and returns the result.  Both original strings are unchanged.
    */
    char * String_Concat (const char *s1, const char *s2) {
      char *sTemp = malloc((sizeof(char) * strlen(s1)) + (sizeof(char) * strlen(s2)));
    
      strcpy(sTemp, s1);
      strcat(sTemp, s2);
    
      return sTemp;
    }
    in main.c
    Code:
    #include "help_fun.h"                  // Helper Functions
    
    const char * const sFlash_Card_Base_Path = "C:\\TC\\HART\\Flash\\"; 
    const char * const sDiagnostic_Log_Name  = "diag_log.txt";
    char   *sDiagnostic_Log_Path;          // Full path of diagnostic log file
    
    int main() {
      // Join sFlash_Card_Base_Path with sDiagnostic_Log_Path to give complete file location
      sDiagnostic_Log_Path = String_Concat(sFlash_Card_Base_Path, sDiagnostic_Log_Name);
    
     retun 0;
    }

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I don't see anything that's problematic off the top of my head other than the fact that you are using malloc() without including stdlib.h. Could that be your error?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Good catch. Funny the Compiler didn't kick that out. I added that include statement, but I still get the same error...

    To be honest, I've never done this before, everything I've ever done has been in one file. I had previously assumed that the compiler would automatically link and compile each #included file.

    I could very well be missing an easy step...

  6. #6
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I really don't see what else could be wrong. What compiler are you using by chance?

    Edit: Oops didnt see Turbo C++ 3.0. I don't know much about that compiler, sorry. GCC/MinGW port and MSVS is what I use.
    Last edited by carrotcake1029; 02-10-2009 at 10:15 PM.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Turbo C++ 3.0

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Interestingly enough, I just copied a project started by someone else. I deleted all the build files (including all the .obj files and the exe) and then opened the project and just ran it. The compiler automatically compiled each .c file into an object, so it must be something within my code that is wrong...

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    My guess is that you're not including help_fun.cpp in the compilation (or, sort of equivalent in effect, not including help_fun.obj in the link). This sort of thing results in complaints about undefined symbols from the linker.

    Also, your malloc() call in String_Concat() needs to allocate one more character, to allow for the trailing zero byte. main() also needs to free(sDiagnostic_Log_Path);

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I just figured it out (before I read your post grumpy, but thanks!). I hadn't added the .c files to the project. Duh...

    Also, your malloc() call in String_Concat() needs to allocate one more character, to allow for the trailing zero byte.
    Does strlen() not count the trailing char?

    main() also needs to free(sDiagnostic_Log_Path);
    I have that in my code, I just didn't copy it over.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by Bladactania View Post
    Code:
    #include "help_fun.h"                  // Helper Functions
    
    const char * const sFlash_Card_Base_Path = "C:\\TC\\HART\\Flash\\"; 
    const char * const sDiagnostic_Log_Name  = "diag_log.txt";
    char   *sDiagnostic_Log_Path;          // Full path of diagnostic log file
    
    int main() {
      // Join sFlash_Card_Base_Path with sDiagnostic_Log_Path to give complete file location
      sDiagnostic_Log_Path = String_Concat(sFlash_Card_Base_Path, sDiagnostic_Log_Name);
    
     retun 0;
    }
    I've experienced the same problem in TC3.0 before and as far as I know that if you include "filename.h" the compiler should already determine that you are directing it to look for the header in the current directory I don't know, sometimes it works sometimes not. It doesn't even give you an option to create a project. Does it says unable to include help_fun.h? if so, maybe you could try to manually locate your header file like this:

    #include "drive\location\location\help_fun.h"

    I hope this could help or maybe could narrow down your problem.
    Last edited by $l4xklynx; 02-10-2009 at 10:47 PM.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Bladactania View Post
    Does strlen() not count the trailing char?
    It does not. strlen("Hello") yields 5, but 6 characters is needed to hold the string "Hello" to allow for the trailing '\0'.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Quote Originally Posted by $l4xklynx View Post
    It doesn't even give you an option to create a project.
    What do you mean it doesn't give you the option to create a project? I created one!

    Quote Originally Posted by $grumpy View Post
    It does not. strlen("Hello") yields 5, but 6 characters is needed to hold the string "Hello" to allow for the trailing '\0'.
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Replies: 6
    Last Post: 04-02-2002, 05:46 AM