LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-10-2004, 11:50 AM   #1
chuanyung
LQ Newbie
 
Registered: Jan 2004
Posts: 25

Rep: Reputation: 15
Question How to use own static library?


Dear all,

I use the following programs and method to create a static library.
But it can't link, what's wrong?

show.h
-------------
#ifndef show_h
#define show_h
#include <iostream>
using namespace std;

void show(char* szString); // use extern void show(char* szString)
// still fail to link

#endif
--------------

show.cpp
--------------
#include "show.h"
void show(char* szString)
{
cout << szString << endl;
}
---------------
main program: test.cpp
---------------
#include "show.h"

int main()
{
char* message="test static library";
show(message);
return 0;
}
-------------------

What I do is:
g++ -c show.cpp
ar cr mylib.a show.o
ranlib mylib.a
g++ -o test -I. -L. -lmylib test.cpp

But g++ don't know what is mylib (mylib.a).
If I use "g++ -o test -I. mylib.a test.cpp",
then "show(char* szString)" is undefined reference.

How should I do?
Thanks for your help.

Regards,
chuanyung
 
Old 03-10-2004, 12:14 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
try:
Code:
g++ -o test -I. test.cpp mylib.a
 
Old 03-10-2004, 01:41 PM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
Or name it libmylib.a, and then either make sure it is in one of your library search paths, or also add -L/path/to/library/which/may/just/be/./
 
Old 03-10-2004, 07:42 PM   #4
chuanyung
LQ Newbie
 
Registered: Jan 2004
Posts: 25

Original Poster
Rep: Reputation: 15
Put mylib.a after test.cpp works fine.
But why?

If I ouput libmylib.a,
use "g++ -o test -I. -L. -lmylib test.cpp"is still failed.
But "g++ -o test -I. test.cpp libmylib.a" works.

I am confused about it.

Regards,
chuanyung.
 
Old 03-10-2004, 07:54 PM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
Try

g++ -o test test.cpp -I. -L. -lmylib

Instead... I've noticed with some libraries, like libGLw.a that if you have the source files after the libraries, it causes link errors...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shared to static library ofada Programming 2 07-08-2009 11:54 AM
static qt library in debian dun_noe Debian 2 05-23-2005 02:42 AM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM
static linking to c library lantern Programming 1 04-19-2004 08:58 AM
How can I use gcc to build a static library? vtluu Red Hat 1 04-12-2004 10:45 AM


All times are GMT -5. The time now is 01:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration