- Online Community Forums » Off-Topic Ordeals » How to build a library or link to something?

This thread is locked; no one can reply to it. rss feed Print
How to build a library or link to something?
Tyler Wrobel
Member #16,594
November 2016

This is my code below. I want to make a library or something that I can link to when compiling. What is the best way to build and .o or library so that I can use it again and again with different projects and as I update the executable also get updated? Basically my code below is just a few of the many functions I plan on coding into this thing.

1#include<iostream> 2#include<string> 3#define msgplain1 "uninitiated" 4#define usramt 1 5#define N N_function() 6using namespace std; 7template <class myType> 8myType *S (myType instance_a,bool line_Space){ 9if(line_Space==true){ 10cout<<(instance_a); 11cout<<endl; 12} 13else{ 14cout<<(instance_a); 15} 16} 17template <class myType> 18myType *S (myType instance_a){ 19cout<<(instance_a); 20cout<<endl; 21} 22template <class myType, class multiplier> 23myType *ML (myType instance_a, multiplier b){ 24for(int i=0;i!=b;i++){ 25S(instance_a,true); 26} 27} 28void *N_function (){ 29cout<<endl; 30} 31string user1 = msgplain1; 32string user2 = msgplain1; 33int varq = usramt; 34int main(int argc, char *argv[]){ 35S <string> (user1); 36ML <string,int> (user2, varq); 37N; 38return 0; 39}

Edgar Reynaldo
Member #8,592
May 2007

First of all what you want depends directly on the compiler you're using. First you need to build .o object files, and then you need to compile them into an archive (*.a) and an import library (*.dll), or just a static archive (still *.a).

For example, with CodeBlocks and MinGW this is the following command given to build a dynamic version of my library EAGLE :

-------------- Build: Debug in Eagle5 (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -shared -Wl,--output-def=cbbuild\bin\libeagle_debug.def -Wl,--out-implib=cbbuild\lib\libeagle_debug.dll.a -Wl,--dll  cbbuild\obj\Dynamic\Debug\src\Animations.o cbbuild\obj\Dynamic\Debug\src\Area.o cbbuild\obj\Dynamic\Debug\src\AVHandler.o 

...40 other source files here

cbbuild\obj\Dynamic\Debug\src\Transforms.o  -o cbbuild\bin\eagle_debug.dll  
Output file is cbbuild\bin\eagle_debug.dll with size 16.76 MB
Process terminated with status 0 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))

The important parts to use with mingw are '-shared', '-Wl,--output-def=$.def', '-Wl,--dll', and the final '-o cbbuild\bin\eagle_debug.dll'.

If you're building a static library with MinGW it's different. You use the same object files you used before, but this time you make a static archive using the 'ar' tool like so :

ar.exe -r -s cbbuild\lib\libeagle_debug-static.a cbbuild\obj\Static\Debug\src\Animations.o cbbuild\obj\Static\Debug\src\Area.o cbbuild\obj\Static\Debug\src\AVHandler.o

... 40 other source files here


ar.exe: creating cbbuild\lib\libeagle_debug-static.a
Output file is cbbuild\lib\libeagle_debug-static.a with size 21.15 MB
Process terminated with status 0 (0 minute(s), 11 second(s))
0 error(s), 0 warning(s) (0 minute(s), 11 second(s))

You need to tell us your Compiler and IDE before we can help you any more.

Member #7,536
July 2006

Firstly, please wrap code in <code></code> tags (XHTML-like) (or `backticks` if inline code) so it's more readable. See my signature to links of the markup features and syntax.

Edgar gave a good introduction to the MinGW command lines. Platform matters too. If you're on Windows you'll likely be using either MinGW or Visual Studio. Edgar already sort of highlighted the MinGW way, though explaining the options would help. Visual Studio should have a special project type for a library (at least, with C# it does).

Typically if you want to build a library for Linux there are two common options: "shared object files" AKA *.so which are dynamically linked at run-time and similar to .dll files in Windows. These are often used because it reduces the size of executables to link libraries at runtime. Alternatively, for ease, you can create a static libray archive AKA *.a which is pretty similar to the MinGW instructions Edgar offered already. The most popular toolchain is probably GCC, but clang is I think attracting people for specialized purposes because it does some things cleaner. I'm only familiar with GCC.

A library command line looks something like this:

# object file; note -c (optional intermediate file).
# To skip instead replace later gcc '.o' arguments with '.c'.
gcc -c -o foo.o foo.c

# shared object (-shared and -Wl,-soname,...)
gcc -shared -Wl,-soname, -o foo.o bar.o. baz.o

# Static "archive" library (can't use '.c' files here because ar is not a compiler).
ar rcs libx.a foo.o bar.o baz.o

You should consult your man or info pages for more details. I pulled these from old makefiles so I'm a bit rusty on some of the details. Not sure what "rcs" is for. :-/ The -c tells GCC to make an object file instead of an executable ELF program. The shared object command options are somewhat self explanatory, though personally I think they're more complicated than they need to be. I'm sure it has history behind it. Note how you have to duplicate the name of the file (but I think one is a "basename", while the other is the actual path). In all cases, "-o path" means to write the output to the file specified by path.

Tyler Wrobel
Member #16,594
November 2016

I am using Emacs in a VirtualBox on a virtual machine running Ubuntu 16.04 LTS and compiling with command line g++.

Gideon Weems
Member #3,925
October 2003

For C, it's easy as heck. First, compile as normal, but add the -fPIC option. Then, compile the object file into a library (basically, what bambams said but with minor addition):

gcc -shared -fPIC -Wl,-soname, -o Library/ ModMusic.o

If you don't feel like putting the resulting library in your library path, run linked programs after exporting LD_LIBRARY_PATH. I don't know how much of this process you would need to change for C++.

Member #7,536
July 2006

Gideon, thanks for mentioning -fPIC. I forgot about that.

C++ is basically the same, except you use g++ instead. C++ mangles the symbols for templates and stuffz so you need to be using the same toolchain for all of your software with C++ to link with a C++ library.

Go to: