User forums > Embedded development

How can I create a static library in Code::Blocks?

(1/2) > >>

xor_NTG:
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.

Jenna:
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.

xor_NTG:
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.

Freem:
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.

xor_NTG:
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
 

--- End code ---


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

--- End code ---

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

Thank you.

Navigation

[0] Message Index

[#] Next page

Go to full version