how to create a user defined library and add functions to it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eldiablo
    New Member
    • Feb 2012
    • 2

    how to create a user defined library and add functions to it?

    i use turbo c++ v3.0.
    i tried to create a library and add the following functions to it.
    int prime(int);
    long fact(long);

    i used the standard procedure for doing so.
    yet when i tried to use my functions after including the header file i had created, a linker error occured.

    linker error : undefined symbol fact(long) in module noname00.cpp.

    now i had used 'int' in my program which means that it had identified the function in my library, yet it refused to work.
    is it because my compiler is outdated or because of some other flaw in the program.
    for the record i had applied the procedure as stated in yashwant kanetkar's book 'Let Us C'.
    please advise on how to correctly create a user defined library.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    When you create a library the output is 2 files, a header to be included into your source code that tells the compiler about the functions in the library and a binary library file (.lib or .a) that is passed to the linker and contains the binary code of the functions in the library.

    If you don't pass the binary library file to the linker you get unresolved (or in your case undefined) symbols because the linker can not find the binary code that the compiler has told it to expect to find.

    So it is not because you tool-set is outdated, it is because you are not passing the binary library file to the linker.

    Comment

    • eldiablo
      New Member
      • Feb 2012
      • 2

      #3
      true.
      I created three files.
      A '.h. file, a '.lib' file and a '.obj' file.
      i put the '.h' file in the include folder and the other two in the lib folder.
      yet the problem persists.
      do i have to include the binary library file too or just save the files in different locations.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        You shouldn't include the binary library that wont work. You need to all it to your link or build command line.

        For example if you were using the following build line

        gcc -Wall -pedantic source.cpp

        you would need to use

        gcc -Wall -pedantic source.cpp -Wl,-l<LibraryFileNa me>

        to pass the library to the link stage of the build.

        Comment

        Working...