Author Topic: static library [SOLVED]  (Read 10565 times)

Offline sspecter

  • Multiple posting newcomer
  • *
  • Posts: 13
  • I feel like I could... TAKE OVER THE WORLD!!!
static library [SOLVED]
« on: February 27, 2006, 03:38:55 am »
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:

Code: [Select]
#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?
« Last Edit: February 27, 2006, 10:13:02 pm by sspecter »

Offline takeshi miya

  • Lives here!
  • ****
  • Posts: 1487
Re: static library
« Reply #1 on: February 27, 2006, 03:55:01 am »
Change
Code: C++
  1. #include "string.h"
to
Code: C++
  1. #include <string>

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: static library
« Reply #2 on: February 27, 2006, 08:06:51 am »
That's not the cause of the error. It is incorrect, but it would probably still work with a deprecation warning.

The cause of the error is that you compile C, and using is no C keyword.
Change the .c file to .cpp.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline sspecter

  • Multiple posting newcomer
  • *
  • Posts: 13
  • I feel like I could... TAKE OVER THE WORLD!!!
Re: static library
« Reply #3 on: February 27, 2006, 12:31:28 pm »
ive change the file to cpp. It work now, But i must include it as cpp.

how do I link a file .a in my project?

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: static library
« Reply #4 on: February 27, 2006, 12:35:37 pm »
Add the library's name to the "Link libaries" field in project options.

Either use the base name (without lib and .a), i.e. if your library is called libfoo.a then you would just enter foo, or use an absolute file name.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline sspecter

  • Multiple posting newcomer
  • *
  • Posts: 13
  • I feel like I could... TAKE OVER THE WORLD!!!
Re: static library
« Reply #5 on: February 27, 2006, 01:21:25 pm »
Its not working.

Im trying 2 things:

- creating the lib .a without header. The program give an error then i try to use it, as if i didnt declared it.

- creating the lib .a with header. The i try to compile the lib, it give the following error:

mingw32-g++.exe   -I"D:\Arquivos de programas\CodeBlocks\include" -c lib\ssptoolslib.cpp -o lib\ssptoolslib.o
mingw32-g++.exe  -L"D:\Arquivos de programas\CodeBlocks\lib" -o lib\ssptoolslib.exe lib\ssptoolslib.o   -lmingw32
lib\ssptoolslib.o:ssptoolslib.cpp:(.text+0x12db): undefined reference to `tools::showMessage(std::string)'
lib\ssptoolslib.o:ssptoolslib.cpp:(.text+0x138a): undefined reference to `tools::showMessage(std::string)'
D:\Arquivos de programas\CodeBlocks\lib/libmingw32.a(main.o):main.c:(.text+0x106): undefined reference to `WinMain@16'
collect2: ld returned 1 exit status

As you can see the lib im trying to create now is not the above as i said before. If you want i can post the code.

is there any tutorial for newbies about creating static libs?

btw: must i create a header for the .a lib or there is no necessity in it?
« Last Edit: February 27, 2006, 01:23:09 pm by sspecter »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: static library
« Reply #6 on: February 27, 2006, 01:31:29 pm »
mingw32-g++.exe  -L"D:\Arquivos de programas\CodeBlocks\lib" -o lib\ssptoolslib.exe lib\ssptoolslib.o   -lmingw32
You are trying to make an EXE file out of a lib. I guess that your type of project is wrong. Make sure if you start a new project you select "Static Library" as project type.
With regards, Morten.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: static library
« Reply #7 on: February 27, 2006, 03:05:36 pm »
Quote
must i create a header for the .a lib or there is no necessity in it?
Not if you declare the functions in the program that links to it. Otherwise, you certainly have to... the compiler won't otherwise know what to do.

Quote
is there any tutorial for newbies about creating static libs?
About 566.000...
http://www.google.com/search?q=c%2B%2B+static+libraries+tutorial
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline sspecter

  • Multiple posting newcomer
  • *
  • Posts: 13
  • I feel like I could... TAKE OVER THE WORLD!!!
Re: static library
« Reply #8 on: February 27, 2006, 05:25:34 pm »
Quote
You are trying to make an EXE file out of a lib. I guess that your type of project is wrong. Make sure if you start a new project you select "Static Library" as project type.
With regards, Morten.

Youre right. the lib with a header that i was creating was in a wrong project. Now i can create the lib without error

But i still cant use it. I get undefined reference then i try to use it.

I know the project is finding the header, because it compile. and i know it is finding the library, because it dont get "cant find -llibssptools.a". But I get undefined reference errors, even when im defining the references in the lib.

the problem appears to be inside the library.
But actually im trying to use Newton library and im getting the same error. So it can be some C::B configuration. But i am already using Ogre and CeGUI libs without error in C::B. So... im clueless.

for people wanting to se the code here are parts of it:

http://pastebin.com/575053
« Last Edit: February 27, 2006, 05:31:27 pm by sspecter »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: static library
« Reply #9 on: February 27, 2006, 05:49:54 pm »
http://pastebin.com/575053
...if I remove the namespace "tools" completely it works fine. Maybe this helps...?!
Morten.

Edit: Here is my main then:
Code: [Select]
#include <iostream>
#include "ssptools.h"

using namespace std;
//using namespace tools;

int main()
{
showMessage("Hello world!\n");
Point3D p(1,2,3);

return 0;
}
« Last Edit: February 27, 2006, 05:52:08 pm by MortenMacFly »
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline sspecter

  • Multiple posting newcomer
  • *
  • Posts: 13
  • I feel like I could... TAKE OVER THE WORLD!!!
Re: static library
« Reply #10 on: February 27, 2006, 07:15:19 pm »
Quote
...if I remove the namespace "tools" completely it works fine. Maybe this helps...?!

Sadly, no.

I removed "using namespace tools;", but then C::B dont recognize any lib references (because im not using the namespace). Ive tried to substitute all references to library with "tools::reference" but then i get the same error as before (undefined references).

I didnt understood why it works for you, or why "using namespace tools" would give an error.
« Last Edit: February 27, 2006, 07:25:31 pm by sspecter »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: static library
« Reply #11 on: February 27, 2006, 07:23:52 pm »
Sadly, no.
Yes, I tried! My project consistst of the library and the main. I guess you didn't also remove the namespace from the class (library). Remove this too and it'll work. You have to remove 4 (or were it 5?) lines of code.
Morten.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: static library
« Reply #12 on: February 27, 2006, 07:24:25 pm »
Hello,

May be you forget to add a file/library to the include/link directory (see Projects-->Build options-->Directories-->Compiler/Link tab).

Best wishes,
Michael

Offline sspecter

  • Multiple posting newcomer
  • *
  • Posts: 13
  • I feel like I could... TAKE OVER THE WORLD!!!
Re: static library
« Reply #13 on: February 27, 2006, 07:39:08 pm »
Quote
May be you forget to add a file/library to the include/link directory (see Projects-->Build options-->Directories-->Compiler/Link tab).

the lib is linked.

As i posted before, Then i dont include the header, C::B dont recognize the references ("identifier not declared" error). Then i dont link the library, i get "cannot find -lssptools" error. I am not getting any of these errors. I even tested to remove the header or the library just to see these errors.

Did I declared the library/header right?
If so, what can give "undefined reference" error, with the library and header included?
Is there anything to do to link a static library besides include the header and link the library?

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: static library
« Reply #14 on: February 27, 2006, 07:51:04 pm »
Hello,

Could you post your project (source files+project file(s)) in a 7z format (www.7zip.org)? I will give it a try this evening.

Best wishes,
Michael