Author Topic: Build a static library and use it in a console application  (Read 4805 times)

Offline pulllo

  • Single posting newcomer
  • *
  • Posts: 2
Build a static library and use it in a console application
« on: March 17, 2012, 04:43:43 pm »
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.
Code
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:
Code
int double(int a)
in mylib.h

Now, i want to use it in a console application file:
Code
#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
« Last Edit: March 17, 2012, 04:52:10 pm by pulllo »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Build a static library and use it in a console application
« Reply #1 on: March 17, 2012, 05:01:07 pm »
New project - Static library.
Code
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:
Code
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++ !

Offline pulllo

  • Single posting newcomer
  • *
  • Posts: 2
Re: Build a static library and use it in a console application
« Reply #2 on: March 17, 2012, 05:57:29 pm »
Oh sorry, it was just an example, my functions are slighltly different :-) For sure i didn't wrote "double", identifiers are legal!
Like

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

Code
int twotimes(int a)

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Build a static library and use it in a console application
« Reply #3 on: March 17, 2012, 07:01:20 pm »
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.