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

sspecter

  • Guest
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
#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 »

takeshimiya

  • Guest
Re: static library
« Reply #1 on: February 27, 2006, 03:55:01 am »
Change
Code: cpp
#include "string.h"
to
Code: cpp
#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."

sspecter

  • Guest
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."

sspecter

  • Guest
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: 9694
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: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://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."

sspecter

  • Guest
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: 9694
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
#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: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

sspecter

  • Guest
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: 9694
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: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://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

sspecter

  • Guest
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

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: static library
« Reply #15 on: February 27, 2006, 07:54:25 pm »
Here is the proof: It works!
Code
-------------- 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).
« Last Edit: February 28, 2006, 11:58:56 am by MortenMacFly »
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

sspecter

  • Guest
Re: static library
« Reply #16 on: February 27, 2006, 08:48:44 pm »
Quote
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.

Quote
Here is the proof: It works!

Wow! youre right!!! Then i removed ALL "namespace" definitions and references, it worked!
Sorry i didnt tested it shortly after your 2nd post. I was testing other things.
This help a lot. Finally were getting somewere :)
Now i just need to know why it cant find the references definitions then I use the namespace. I am making a program that uses lots of libraries, and I really want use namespaces.

Do static libraries accept namespaces?

Quote
Ps.: Please notify if I can remove the ZIP file again to save forum webspace...

sure :) i already tested here

Quote
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.

The project is attached, with the namespaces. I simplified it at maximum, so it is much cleaner than the code i posted before. The file is very small, and i dont have the 7z, so i posted in zip. Hope you dont mind.

Just open the staticlib.workspace and build the console application project. The other project is the static library, it is already built.


[attachment deleted by admin]
« Last Edit: February 27, 2006, 08:58:19 pm by sspecter »

sspecter

  • Guest
Re: static library
« Reply #17 on: February 27, 2006, 09:11:53 pm »
I finally found the error :)

I actually had to declare namespace in the header and the library, but I was just declaring it in the header, and calling it in the library. Because of that something got messed up. I still didnt understand  what happened with the references then i done that.

Thanks guys, you were really helpfull.

I just have 1 last question:
If you saw my library code you noticed I comented the cout use "cout << msg << endl;". Its because C::B dont recognize cout when i use it in the library. How can I use cout in a library? Or theres another way to output to the console? printf dont recognize my type string :P.

sethjackson

  • Guest
Re: static library
« Reply #18 on: February 27, 2006, 09:42:12 pm »
Code: cpp
std::cout << "Hello World!"; 

Will work. You could also do

Code: cpp
using namespace std; 

cout << "Hello World!";

I always use the first example though......

sspecter

  • Guest
Re: static library
« Reply #19 on: February 27, 2006, 10:11:17 pm »
I just forgot to include iostream.  :oops:

Anyway this thread is SOLVED  8)

Thanks again.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: static library
« Reply #20 on: February 28, 2006, 11:38:11 am »
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:

Quote
namespace tools{
...
};

Then you can use using namespace tools; in the main.cpp.

Code
#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