Thread: Libraries

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    15

    Libraries

    Hello all. I'm trying to make static libraries on DevCpp, but i'm having many troubles(at least I've made them, but not as I'd like).
    1-I create new project (MyProject), static library. The first page I declare a simple function int hello() which contains printf("hello"). Then, save it as libfunc.c. Then in the same project I create a new file, with #include <stdio.h> and the hello function prototype int hello(); and save it as funcheader.h and then I compile the project. Automatically, in my desktop appear the following files

    libfunc.c, MyProject.dev, libhello.o, funcheader.h, MyProject.a, Makefile.win, gmon.out

    I understand where libfunc.c, funcheader.h (the source files of the project) and MyProject.dev(the project) come from, but after compiling the project, i'm only suposed to get a "libXXX.a" library, not gmon, libhello, makefile. What are those things?

    2-I don't want to use the project manager to make a library, I'd like to make it in notepad, or at most, on simple "new blank pages" in DevCpp. How could I simply make it and then tell the compiler that the first page i want to convert to object code, and compile the header + the object code into a library?

    3-I'm a little worried about the #includes. Am I including them right in the header? or should I include them in the .c source where the functions are declarated? Is there any difference between declaring them in one or other side? Same happens with #defines. I've read about #ifndef and those commands, recommended because some sort of problem related about what i've explained in this point.

    Well many thanks to anybody who helps me. Of course, i've browsed all arround the web but no results will help me.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Every project includes a makefile, which tells the compiler how to compile whatever it is to compile.

    You cannot have an .a file without first having an .o file.

    gmon.out is used for profiling -- it's the output of gprof. So you may have that set somewhere, or at least enabled.

    You should include in the header only those files needed for the header, not for the code itself -- anybody who wants to use your library will include your header, and there's no reason to include something there for your code (it's already compiled).

    To find out how to make a library, look at the makefile -- that's the complete specification of what the compiler is doing.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Hi habstop, thanks for the explanation. I understand now why makefile. But my question now is, as I think happens with Linux console compiler, is there any way to say the compiler in c "compile it as object", or "compile it as c source", or tell the linker "link those files as library". If necessary, i'd run DevCpp compiler and linker separately and out from the DevCpp IDE.

    About including, tell me if i'm right, if I want to make a library with a function "int hello()" , i would open a static library project (named hello.dev), inside, in one blank file i'd write just
    Code:
    int hello(){printf("hello")}
    and save it as .c, then create a new file again inside the project named hello.h, and write "int hello()". Just that, then i'd compile and the library would happen, no #include <stdio.h> needed at all. Is this ok? I think my trouble was cause I tried not to save, but compile the c source of the library, so the linker asked me the stdio in order to use printf.

    Also, if you know, please could you tell me, how for example printf is declared? I mean, if i would remove all not strictly necessary to run DevCpp or the compiler, how could I make a function? Is then needed assambler? cause I'd like to learn assambler too, but all manuals I find are about 16 bit computers, and today computers are much more advanced(64-bit, two core, 2,xx GHz...), don't know if the same assambler would work here.

    Again, thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can tell the compiler where to stop, certainly. There are four stages to building a program: preprocessing, compiling, assembling, and linking. You can go as far down the list as you need to. Usually, if you're building a library, or just compiling "part of" a program, then you stop at assembling and don't link.

    In your library example, the file with the function in it would need #include <stdio.h>, 'cause that function uses printf, and printf needs stdio.h. The main program may or may not <stdio.h>; it wouldn't need it just for that function call.

    You can look up the function header for printf, certainly; it's
    Code:
    int printf(const char * restrict format, ...);
    However, it is illegal in C to declare this function yourself (yes even if you use the exact same declaration and intend to use the library function); use the stdio.h header instead.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Ok, but if I include the stdio.h there, i don't know the name of this, but i've read about "duplicating" the libraries and defines in the main program or in self libraries. Suppose I'm going to use printf in the main program, so I include the stdio.h header. Now, when linking, there will be two stdio libraries (or more if to make my library I used two source files, each one with one function containing printf and thereby include a stdio.h in them). To avoid this, i've read about some #ifdef, #ifndef, and some words with down bars like "__word_". Does anything of this have to do with libraries? Thanks tab

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kiknewbie View Post
    Ok, but if I include the stdio.h there, i don't know the name of this, but i've read about "duplicating" the libraries and defines in the main program or in self libraries. Suppose I'm going to use printf in the main program, so I include the stdio.h header. Now, when linking, there will be two stdio libraries (or more if to make my library I used two source files, each one with one function containing printf and thereby include a stdio.h in them). To avoid this, i've read about some #ifdef, #ifndef, and some words with down bars like "__word_". Does anything of this have to do with libraries? Thanks tab
    No.

    The library gets linked in once, during the linking phase. Each file can include the header file once, and in fact each file may well need to include the header file. There's no code in header files, though.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Nice, so this is solved. Thanks a lot tab. I'll start now a new thread about how to compile just to object from c, 'cause all I know is the DevCpp compiler is something about g++, or c++ or any of that. Bye.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC: Compiling with both static and shared libraries
    By eatwithaspork in forum C Programming
    Replies: 4
    Last Post: 06-23-2008, 01:48 PM
  2. Replies: 7
    Last Post: 05-13-2008, 03:47 AM
  3. MinGW thread-safe runtime libraries
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2006, 08:15 AM
  4. Libraries and headers
    By darksaidin in forum C++ Programming
    Replies: 10
    Last Post: 07-23-2003, 06:24 AM
  5. QT and external libraries
    By Quacker in forum Linux Programming
    Replies: 1
    Last Post: 04-08-2003, 07:02 AM