-1

Just a quick question, that I'm sure will be easy for people on this forum to answer.

How would I go about creating a static library, which I believe is a dll (but I am most likely wrong :S ), in c++. I've just started using c++ but I'm getting quite annoyed with moving my functions to every new program that I make, so how would I go about making a library that I could reference each time?

Thanks for any help given, I really appreciate it.

MobA11y
  • 18,425
  • 3
  • 49
  • 76
fishycrakers
  • 71
  • 3
  • 10
  • 2
    DLL would not be static, it would be dynamic. Statically linked and dynamically linked libraries are slightly different. If you clarify your question as to which you want, I'd be happy to help. – MobA11y Jun 18 '13 at 14:12
  • 2
    What compiler are you using? – ZioN Jun 18 '13 at 14:13
  • 2
    DLL stands for Dynamic Link Library. "Dynamically" in these cases usually means "at runtime". It's pretty much the opposite of a static library, which is linked at compile time. – Theodoros Chatzigiannakis Jun 18 '13 at 14:14
  • I would like to create a library that I can call my functions from. Would a static library be best for that? – fishycrakers Jun 18 '13 at 14:15
  • It's easiest. The error messages you get from static linking are more helpful than the runtime errors you can get with dynamic libraries. – MobA11y Jun 18 '13 at 14:16
  • _Would a static library be best for that?_ It depends. At least for your 1st steps, you should choose the easier way, which ist using a static library. – πάντα ῥεῖ Jun 18 '13 at 14:17
  • In general, static libraries should be preferred for your own libraries, since once linked, they become part of the executable. – James Kanze Jun 18 '13 at 14:42
  • check this http://stackoverflow.com/questions/5947067/how-to-create-a-static-library-with-g – tkanzakic Jun 18 '13 at 16:45

1 Answers1

1

I believe what you want is to use the -c flag. What this does is basically allow you to compile an object file, without the need for a main function. You can then use this object file in any of your programs, you just need to include the header file, so your new objects can compile. They will then link to this .o file. You could also consider turning this into a .a file, but it is an unnecessary step. The only real difference between .a and .o files is how the data is stored, and the compilation commands that take advantage of them. This would be the typical series of commands for taking advantage of a .o file.

STEP1: First build the .o file, with the -c flag, which at the most basic level lets the compiler know that the lack of a main is okay.

COMMAND1: g++ -c someLib.cpp -o someLib.o

STEP2: Now combine the objects from our library and my cpp file that wants to use the "library" into the same program.

COMMAND2: g++ someMainFile.cpp someLib.o -o someMainProgram

The benefit of dynamic linking over this process would be that you don't have symbol duplication. In the case above the symbols that exist in someLib.o will also end up existing in someMainProgram (EX: if you compiled then deleted all occurances of someLib.o from your system, your program would still run!). If they were in a DLL, someMainProgram would only have the symbols in someMainFile.cpp, and it would attempt to find the symbols in someLib at run time, amongst the available dlls.

MobA11y
  • 18,425
  • 3
  • 49
  • 76
  • Could you be more descriptive please. I am an absolute beginner. How do I 'build the .o file with the -c command' and where do I build these? Again with the combination. I appreciate the help, but this makes little sense to me. Sorry – fishycrakers Jun 18 '13 at 14:34
  • @ChrisCM Did you mean to put the first `-o` flag in your second command, just after the `g++`? Or is that some usage of the flag that I don't know? – ajp15243 Jun 18 '13 at 14:39
  • That shouldn't be there, you are correct, thanks! – MobA11y Jun 18 '13 at 14:39
  • @OP: I really don't know how I could be more descriptive... this is really a step by step process on how to do you what want. It is only up to you to supply the content of someLib.cpp and someMainFile.cpp. If you could ask a specific question about what is confusing you... It might help for you to know that, right below each instruction is the command line that will do what the instruction is telling you to do... – MobA11y Jun 18 '13 at 14:41
  • That still doesn't build a library. – James Kanze Jun 18 '13 at 14:41
  • @JAMES: It replicates the results the OP wants in the simplest process possible... – MobA11y Jun 18 '13 at 14:43
  • What I want is to make a library, from scratch, in visual basic. This way I can call my function, from the library, at the start of my code using the preprocessors '#include' statement. – fishycrakers Jun 18 '13 at 16:13
  • Not VB, sorry, Visual C++ ^^, That was embarassing – fishycrakers Jun 18 '13 at 16:54
  • @ChrisCM Would your solution be put in the file header? Because this is whats confusing me, where does it go? – fishycrakers Jun 18 '13 at 16:56
  • @OP: You're dealing with visual studio. I was assuming a unix environment, I am not familiar enough with visual studio to comment. – MobA11y Jun 18 '13 at 17:07
  • @ChrisCM This is most likely why I could not comprehend your post :( Sorry, I probably should've stated in my original post that I was using Visual C++ – fishycrakers Jun 18 '13 at 17:36