Author Topic: How can I create a static library in Code::Blocks?  (Read 35481 times)

Offline xor_NTG

  • Single posting newcomer
  • *
  • Posts: 7
How can I create a static library in Code::Blocks?
« on: October 12, 2012, 09:46:51 pm »
Hello everybody!

I'm new on this forum so I have to present myself: I'm David, 17 years old and I am programming for almost 2 years in C++ (this is the primary language). I can handle a bit PHP, Python, C, C# and Visual C++ and that's all until now. If you continue reading this, please excuse my english, I'm not from UK or US so english is not my basic language. I am learning it.

I am working in Code::Blocks since I started to program. I can say that is a tremendous IDE, very intelligent (especially in syntax highlighting).

Now, I have to create a library (about vectors, but this is not important now).
Well, I saw that I can create that in Code::Blocks...so I did like that: New Project -> Static Library

Here, I see a file, called main.c with some functions in it. I can see a function wich returns the sum of 2 numbers. I rename it "Add". Now, I create a header file, named Add.h and I attach it to that project. I put it that header file, the prototype of the Add function. I compile the project. Now, the IDE gives me a libStatic_Library.a file located near the main.c file. Static_Library is the name of the project. As far as I learned, that file, libStatic_Library.a is a library - file, so my function "Add" is in that file.

Now I create a new Console Application project, in C++ language, I attach the Add.h header file to that project AND I link the library (or I at least I think that) like that:

Project -> Build Options -> Linker Settings -> Add, and here I browse the libStatic_Library.a file, and when I double - click on it, it says: "Keep this as a relative path? Yes/no". I click Yes, and then OK. Now (I think) the library should be linked.

I the main.cpp file I add the header file, #include"Add.h" and in main() function, I write that: std::cout<<Add(3,4); When I compile, I have an error, which says like that: undefined refference to 'Add(int, int)'.


What am I doing wrong?


I notice you that I am trying to solve this problem for a few weeks, but no result. Below, I tried to explain you every step I do, so that you can see if I am doing something wrong or if I am missing something.

P.S: All that I want is to create a library with a function in it, and to link it in Code::Blocks.

Thank you respectfully.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: How can I create a static library in Code::Blocks?
« Reply #1 on: October 12, 2012, 11:39:34 pm »
You mix a C library with a C++ program, so you run into name mangling issues.

Search the forum and/or the web for "name mangling" and you will get several ways to fix this issue.

I could explain it here, but there are so many articles about that, which explain it much better.

Offline xor_NTG

  • Single posting newcomer
  • *
  • Posts: 7
Re: How can I create a static library in Code::Blocks?
« Reply #2 on: October 13, 2012, 11:57:45 am »
Hello! Thank you for your answer.

I some way, I fixed the problem but I'm not fully satisfied.

I searched on the web "name mangling" and I found a website which said like that: write that: extern "C" { function_prototypes... }; in the header file and will work.

I did exactely like that, and it worked. But if I want to create a library with a C++ contents in it, it's a bit tricky. So, I created another Static Library project and I replaced that main.c with a main.cpp file (because my functions requires some C++ objects, for instace cout<< and cin>> and I have to include<iostream> for them). I've also created a header with the function prototypes in it. I've created a Console Application project and I've add that header file, I linked the library and I get an error which says: cannot find -lvector_int.  vector_int is the name I gave to my library.

A very important notice is that...my functions (which are written in that main.cpp file) are actually written in a structure. More exactely, they are written like that:

struct vector_int
{     
     void function1(int a, int b);
     int function2(int x);
     ..........................;
};

Now, I've put the function prototypes in the header file. How can I specify that they are written in a structure?

Thank you respectfully.

Offline Freem

  • Almost regular
  • **
  • Posts: 219
Re: How can I create a static library in Code::Blocks?
« Reply #3 on: October 14, 2012, 02:24:08 pm »
Could you give the full compiler log, please?

If I have to reply without seeing it, I would say that you actually did not indicate to C::B where to find your vector_int lib.

Offline xor_NTG

  • Single posting newcomer
  • *
  • Posts: 7
Re: How can I create a static library in Code::Blocks?
« Reply #4 on: October 14, 2012, 02:50:41 pm »
Of course.

Build log:

Code
-------------- Build: Debug in Test_VECTOR_INT ---------------

Linking console executable: bin\Debug\Test_VECTOR_INT.exe
obj\Debug\main.o: In function `main':
C:/CODE_BLOCKS_PROJECTS/Test_VECTOR_INT/main.cpp:9: undefined reference to `vector_int::citire()'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings
 


My vector_int.h file:

Code
#ifndef VECTOR_INT_H_INCLUDED
#define VECTOR_INT_H_INCLUDED


  // vector_int function prototypes
struct vector_int {
     void sort_par(void);
     void sort_d(void);
     void sort_c(void);
     bool unicitate_vector();
     void adaugare_element(int a);
     void inserare_element(int a, unsigned int pozitie);
     int sterge_element_total(int a);
     int sterge_element(unsigned int pozitie);
     void sterge_element_aux(unsigned int pozitie);
     int numar_aparitii(int a);
     int afisare_element(int pozitie);
     int cauta_element(int a);
     int nr_elemente(void);
     void salvare_fisier(void);
     void preluare_fisier(void);
     void afisare(void);
     void citire(void);
};



#endif

I don't know if I declared properly that structure.

Thank you.

Offline Freem

  • Almost regular
  • **
  • Posts: 219
Re: How can I create a static library in Code::Blocks?
« Reply #5 on: October 14, 2012, 04:55:31 pm »
This is not the full compiler log. Please read this http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F for instructions about how to enable it.

Offline xor_NTG

  • Single posting newcomer
  • *
  • Posts: 7
Re: How can I create a static library in Code::Blocks?
« Reply #6 on: October 14, 2012, 07:10:15 pm »
Ok. I did exactly as they said on that site and I have that compiler build log:

Code
-------------- Build: Debug in Test_VECTOR_INT ---------------

mingw32-g++.exe  -o bin\Debug\Test_VECTOR_INT.exe obj\Debug\main.o    -lvector_int
obj\Debug\main.o: In function `main':
C:/CODE_BLOCKS_PROJECTS/Test_VECTOR_INT/main.cpp:9: undefined reference to `vector_int::citire()'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings
 

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: How can I create a static library in Code::Blocks?
« Reply #7 on: October 15, 2012, 07:37:01 am »
Ok. I did exactly as they said on that site and I have that compiler build log:
Code
C:/CODE_BLOCKS_PROJECTS/Test_VECTOR_INT/main.cpp:9: undefined reference to `vector_int::citire()'
collect2: ld returned 1 exit status
Without access to the SDK nobody will be able to help you. We can only guess and this will lead to nowhere. Please insp3ct the SDK's documentation or ask the developers of this library how to use it with MinGW.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline xor_NTG

  • Single posting newcomer
  • *
  • Posts: 7
Re: How can I create a static library in Code::Blocks?
« Reply #8 on: October 16, 2012, 08:37:21 pm »
I've created this library.

Offline cacb

  • Lives here!
  • ****
  • Posts: 536
Re: How can I create a static library in Code::Blocks?
« Reply #9 on: October 17, 2012, 12:02:11 am »
Your question is really unrelated to C::B.It has to do with understanding C++ and the difference between C++ and C.

In C++, a struct is the same as a class (except all is by default public). Look it up in a C++ book. You can put C++ classes in libraries, no problem, but it isn't related to C::B