How to create a library in Code::Blocks

Hello!

I want to create a library. I searched on google, but nothing appropriate came out. I don't want to make something complex, just some math - functions (for instance the sum of two numbers or something like that) but I want to be created by me. I want you to tell me if this is posible to do in Code::Blocks, or if I need other applications.

Some useful links will be appreciated.

Thank you respectfully.
Any ideas?
You just make a header file with your functions definitions and code and include it, that's the easiest and simplest way. make sure its in the same directory as main.cpp
Last edited on
Open Code::Blocks, select File->New->Project, scroll down to Static Library, select it, click Go and then follow the instructions on the screen. ( I am not sure about this, I never worked with it myself. )
Thank you guys. The Zephilinox's method is the easiest. I also tried the Fransje's method. I did exactly as the wizard said and then I found in the project a file with the name I gived to the library but with the extension .a Also, the file in which I wrote the functions was a .c file. So if I link the .a file in a C++ program, everything will be ok or that .a file work just on C programs?

Thanks.
Last edited on
As I thought...isn't possible. So I've created a C++ program, I wrote a simple code which contained a call of a function from that library. I went to Project -> Build Options -> Linker Settings -> Add, I browsed for that file located near the main.cpp. When I try to run it, the compiler says that the function called is not declared in this scope. Any ideas?
Did you include the function prototype (usually done in a header file)?
Allright, allow me to be more specific:

I have the main.c file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// The functions contained in this file are pretty dummy
// and are included only as a placeholder. Nevertheless,
// they *will* get included in the static library if you
// don't remove them :)
//
// Obviously, you 'll have to write yourself the super-duper
// functions to include in the resulting library...
// Also, it's not necessary to write every function in this file.
// Feel free to add more files in this project. They will be
// included in the resulting library.

// A function adding two integers and returning the result

int Add(int i1, int i2)
{
    return i1 + i2;
}

// A function doing nothing ;)
int Substract(int a, int b)
{
   return(a-b);
}

// A function always returning zero
int check_function(int a)
{
    if(a%2==0)
    return 1;
    else
    return 0;
}


When I compile this, it creates me the .a file, near the main.c
If I try to run this, it says that "You must a host application to "run" a library".

Now, the main.cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>

using std::cout;

int main()
{
    int a,b;
    a = 3;
    b = 4;

    cout<<Add(a,b);
    return 0;
}

I link the library as I said earlier.
When I compile this, it says that Add function is not declared in this scope.

I understand that I have to put a function prototype somewhere, but where?

Please excuse me if I put too many questions but I am a beginner at working with libraries so now I am learning.

Thank you respectfully.
I understand that I have to put a function prototype somewhere, but where?


The compiler must know about the function before it has to call it. To do this you either declare it before it is used, or you define it before it is used.

Because you are using a library, the definition is in that library, so you must declare it before it is used. Anywhere you like. This, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>

using std::cout;

int main()
{
    int a,b;
    a = 3;
    b = 4;

    int Add(int i1, int i2);
    cout<<Add(a,b);
    return 0;
}


However, ramming them in the middle gets untidy, so how about at the top?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
int Add(int i1, int i2);

using std::cout;

int main()
{
    int a,b;
    a = 3;
    b = 4;

   
    cout<<Add(a,b);
    return 0;
}


In fact, let's declare all of them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
int Add(int i1, int i2);
int Substract(int a, int b);
int check_function(int a);

using std::cout;

int main()
{
    int a,b;
    a = 3;
    b = 4;

   
    cout<<Add(a,b);
    return 0;
}


You know, even that seems a big messy, and it requires the person using the library to manually type in the declarations of functions. What if they get it wrong? Or forget one? Let's make a new header file:

functions.h
1
2
3
int Add(int i1, int i2);
int Substract(int a, int b);
int check_function(int a);


and then we can just #include that header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include "functions.h"

using std::cout;

int main()
{
    int a,b;
    a = 3;
    b = 4;

    cout<<Add(a,b);
    return 0;
}


Hey, just like all the other headers! It declares all the functions and we don't have to worry about it. Now, if you ever distribute the library, you can distribute the compiled library, and the header file, and everyone will easily be able to use the library.

This is why we have header files, and it's why libraries come with header files to #include.
Wow! The explanation is amazing. I think you would be a highly appreciated teacher (if you are not already). I understood all you explained. I did exactly as you said but I am still making something wrong. And I don't know what.

This time is about the linking the library. The library I created is named libLibrary.a I've named it just "Library" but the IDE has put an 'lib' in front of it.

Anyway, when I go to Project -> Build Options -> Linker settings -> Add, Browse and I double - click on the libLibrary.a (placed near the main.cpp) it says like that: 'Keep this as a relative path?' Yes / No. If I press Yes, when I compile main.cpp it says: cannot find -lLibrary
If I press no when I compile, it says: undefined refference to Add(int, int).

What should I do with this?
Last edited on
Undefined reference means that your code is fine, but it cannot find the compiled function code when it goes looking for it (i.e. the library is not linking). As I recall, under Visual Studio, you have to specify the directory to look for libraries in one place, and the filenames of the libraries in a different place.
Allright but I stiil can't solve this. I checked the prototpype, the definition of the function but stiil the same error. Should I put the .a file in a different folder? Or in the Debug folder?
Are you using windows? Typically, static libraries are *.lib under windows.

Anyway, things to check are:

You have told the linker where to find the library.
You have told the linker the name of the library (if you're using windows, this should be a *.lib file - under *.nix, a *.a file).
You are calling the function correctly (i.e. the name is exactly correct, and you're passing in exactly the right kind of parameters).

One or more of the above is not set correctly.

I am on windows but the library is a .a file. That is the extension that Code::Blocks created. How can I modify that in a .lib file? Maybe this is the problem (the wrong file extension). Would it have any effect if I simply rename the .a file by placing .lib at the end (I supose not)?
MinGW uses ar archives for static libraries on Windows as well. This is not a problem.
You need to make sure the archive's name starts with lib, e.g. libYourLibrary.a. In the linker options, you then have to add "YourLibrary" under link libraries and the directory to the linker search directories (you can specify relative paths, e.g. . or lib - they're relative to your project's directory).
Last edited on
Topic archived. No new replies allowed.