News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

Build a static library and use it in a console application

Started by pulllo, March 17, 2012, 04:43:43 PM

Previous topic - Next topic

pulllo

Hi everybody. I know my question is trivial, but i didn't find on the net a correct answer or a tutorial for my problem.
I wrote some very simple functions and i'd like to embed them in a library. I want to use them in a console application (possibily not in a project). I use C++ with codeblocks.
I tried to do this:

New project - Static library.
int double(int a)
{return 2*a;}

and compiled it. It works, and i had the mylib.a file. But didn't get the .h file, so i wrote it:
int double(int a)
in mylib.h

Now, i want to use it in a console application file:

#include<iostream>
#include"mylib.h"
using namespace std;
int main()
{ cout << double(10);}

but the compiler can't find the function "double".
All files are in the same directory (the third one is a .cpp file).

If it's possibile, i want to keep the second step (use the library in the application) as simple as possibile, because I have to use this things in a course of C++ for beginners. I found some solutions, but they were all using gui applications or project; we won't use such tools.


Thankyou
Giulio

Jenna

Quote from: pulllo on March 17, 2012, 04:43:43 PM
New project - Static library.
int double(int a)
{return 2*a;}

and compiled it. It works, and i had the mylib.a file. But didn't get the .h file, so i wrote it:
int double(int a)
in mylib.h

How did you make this work ?

This is not legal code, at least not in c or c++ !

pulllo

Oh sorry, it was just an example, my functions are slighltly different :-) For sure i didn't wrote "double", identifiers are legal!
Like

int twotimes(int a)
{return 2*a;}


int twotimes(int a)

Jenna

You will not be able to link to any lib without a project or by  changing the global compiler settings.
But this will break all other programs you compile with the compiler sooner or later.