Using Libs

Hello.
From File>New>Project>Static lib> CodeBlocks makes automatically the liba.a library at C:...Desktop\New Folder\a\bin\Debug dir, that contains only ...

1
2
3
4
int SampleAddInt(int i1, int i2)
{
    return i1 + i2;
}

by default.

Then from File>New>Project>Console application i made the main.cpp in c++ language with code...

1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
    cout << SampleAddInt(1,3) << endl;
    return 0;
}



From Build options>debug>Link libraries>Add>, choosing ..\a\bin\Debug\liba.a ...
When compiling i have problem

'SampleAddInt' was not declared in this scope

any idea?


In main.cpp, you need to #include the .h file that contains the declaration of SampleAddInt.
If you don't have one, you can make one yourself:
1
2
3
4
5
6
7
8
9
// Apparently, you named your library 'a'
// So name this file 'a.h'

#ifndef A_H_INCLUDED // Or pick a different identifier; doesn't really matter
#define A_H_INCLUDED

int SampleAddInt(int i1, int i2);

#endif 
and then #include "a.h" (but make sure that it's in the same directory as main.cpp).

Or, if you're too lazy to do any of that, just put
int SampleAddInt(int i1, int i2);
on line 4 of your main.cpp.
Ok thanks
Trying second the easy way i have this...

undefined reference to `SampleAddInt(int, int)

Same for first method with a.h ...

First project by default is in c and second in c++.
When i try only in c there is no problem, but in c++ how?




Surround function definition in "extern "C" and it will work. C++ supports function overloading, so the C++ compiler changes function names.
I don't make it again...
how i do it?
Topic archived. No new replies allowed.