User forums > General (but related to Code::Blocks)
Build a static library and use it in a console application
(1/1)
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.
--- Code: ---int double(int a)
{return 2*a;}
--- End code ---
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)
--- End code ---
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);}
--- End code ---
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.
--- Code: ---int double(int a)
{return 2*a;}
--- End code ---
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)
--- End code ---
in mylib.h
--- End quote ---
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
--- Code: ---int twotimes(int a)
{return 2*a;}
--- End code ---
--- Code: ---int twotimes(int a)
--- End code ---
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.
Navigation
[0] Message Index
Go to full version