Hi.
I am having a newbie problem with static library, but as i think it has not much to do with C::B i am posting here in general forum.
i am trying to find a way to use string in a static library (.c) but nothing that i try work.
thats the last thing im trying:
#include "string.h"
using namespace std;
string SampleAddInt(int i1, int i2)
{
return i1 + i2;
}
the error:
Syntax error before namespace.
I already tried to use std::string with no luck.
How can i use string in a static library?
http://pastebin.com/575053 (http://pastebin.com/575053)
...if I remove the namespace "tools" completely it works fine. Maybe this helps...?!
Morten.
Edit: Here is my main then:
#include <iostream>
#include "ssptools.h"
using namespace std;
//using namespace tools;
int main()
{
showMessage("Hello world!\n");
Point3D p(1,2,3);
return 0;
}
Here is the proof: It works!
-------------- Build: default in ssptools ---------------
mingw32-g++.exe -IE:\Devel\CodeBlocks\include -c ssptools.cpp -o .objs\ssptools.o
ar.exe -r libssptools.a .objs\ssptools.o
ranlib libssptools.a
ar.exe: creating libssptools.a
-------------- Build: default in cb ---------------
mingw32-g++.exe -Ilib -IE:\Devel\CodeBlocks\include -c main.cpp -o .objs\main.o
mingw32-g++.exe -Llib -LE:\Devel\CodeBlocks\lib -o cb.exe .objs\main.o -lssptools
Process terminated with status 0 (0 minutes, 4 seconds)
0 errors, 0 warnings
I've attached a ZIP file containing the demo project for you. I hope this will help.
With regards, Morten.
Ps.: Please notify if I can remove the ZIP file again to save forum webspace...
Edit: ZIP file removed (issue solved).
std::cout << "Hello World!";
Will work. You could also do
using namespace std;
cout << "Hello World!";
I always use the first example though......
Do static libraries accept namespaces?
Hello,
I have slightly modified the projects poseted by MortenMacFly to include the namespace tools. Moreover, I have remarked that there were several declarations using namespace std;. Not all are necessary.
To make it work with the namespace tools, you have to wrap not only ssptools.h, but also ssptools.cpp with:
namespace tools{
...
};
Then you can use using namespace tools; in the main.cpp.
#include <iostream>
#include "ssptools.h"
using namespace tools;
int main()
{
showMessage("Hello world!\n");
Point3D p(1,2,3);
return 0;
}
If you forget to wrap the ssptools.cpp, then it will not works, because in the library there are not the "good" methods. Therefore, you were getting undefined errors.
Also the using namespace std; in the main.cpp and ssptools.cpp are not necessary.
Best wishes,
Michael