Code::Blocks Forums

User forums => Embedded development => Topic started by: xor_NTG on October 12, 2012, 09:46:51 pm

Title: How can I create a static library in Code::Blocks?
Post by: xor_NTG 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.
Title: Re: How can I create a static library in Code::Blocks?
Post by: Jenna 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.
Title: Re: How can I create a static library in Code::Blocks?
Post by: xor_NTG 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.
Title: Re: How can I create a static library in Code::Blocks?
Post by: Freem 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.
Title: Re: How can I create a static library in Code::Blocks?
Post by: xor_NTG 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.
Title: Re: How can I create a static library in Code::Blocks?
Post by: Freem 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.
Title: Re: How can I create a static library in Code::Blocks?
Post by: xor_NTG 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
 
Title: Re: How can I create a static library in Code::Blocks?
Post by: MortenMacFly 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.
Title: Re: How can I create a static library in Code::Blocks?
Post by: xor_NTG on October 16, 2012, 08:37:21 pm
I've created this library.
Title: Re: How can I create a static library in Code::Blocks?
Post by: cacb 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