LinuxQuestions.org
Help answer threads with 0 replies.
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 01-04-2005, 10:15 PM   #1
exitsfunnel
Member
 
Registered: May 2003
Distribution: RedHat
Posts: 60

Rep: Reputation: 15
g++ trouble linking against archive


Hello,

I've got the following simple source files:

//Begin fun1.cpp
#include <iostream.h>
void fun1( ) { cout << "Exits in fun1( )\n"; }
//End fun1.cpp

//Begin fun2.cpp
#include <iostream.h>
void fun2( ) { cout << "Exits in fun2( )\n"; }
//End fun2.cpp

//Begin funmain.cpp
#include <iostream.h>
void fun1( );
int main( )
{
cout << "about to call fun1( )\n";
fun1( );
cout << "just called fun1( )\n";
return 0;
}
//End funmain.cpp

all of the source files are in the directory /home/exits/moment/cpptest. I compile the two functions like this:

g++ -c fun1.cpp
g++ -c fun2.cpp

I then archive the two resulting object files like this:

ar -cr libfun.a fun1.o fun2.o

So far so good (I think) i've not got my library containing the two functions. I can't seem to figure out though how to link my main method agains the archive. It seems this should work:

g++ -o fun -L/home/exits/moment/cpptest -lfun funmain.cpp

but it doesn't instead g++ complains about an undefined reference to fun1( ). If I just pop the .o file on the command line when I compile/link main everything is fine but I really need to get the library stuff working. If anyone could point out what I've done wrong, I'd really appreciate it. Thanks in advance!

-exits
 
Old 01-04-2005, 11:58 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
The -l switch is for linking in shared libraries. ar creates static libraries.

EDIT: Nevermind, forget what I said.

Last edited by itsme86; 01-05-2005 at 12:01 AM.
 
Old 01-05-2005, 12:07 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I got this to work:
Code:
itsme@dreams:~/C/temp$ cat fun1.c
#include <stdio.h>

void fun1(void)
{
  puts("In fun1");
}
Code:
itsme@dreams:~/C/temp$ cat fun2.c
#include <stdio.h>

void fun2(void)
{
  puts("In fun2");
}
Code:
itsme@dreams:~/C/temp$ cat fun.c
extern void fun1(void);
extern void fun2(void);

int main(void)
{
  fun1();
  fun2();

  return 0;
}
Code:
itsme@dreams:~/C/temp$ gcc -Wall -c fun1.c
itsme@dreams:~/C/temp$ gcc -Wall -c fun2.c
itsme@dreams:~/C/temp$ ar -cr libfun.a fun1.o fun2.o
itsme@dreams:~/C/temp$ gcc -Wall -L. fun.c -o fun -lfun
itsme@dreams:~/C/temp$ ./fun
In fun1
In fun2
itsme@dreams:~/C/temp$
EDIT: I think the problem is that you have the -lfun before the funmain.cpp in your compile line. Try switching those 2 around and see if it works okay for you.

Last edited by itsme86; 01-05-2005 at 12:09 AM.
 
Old 01-05-2005, 09:16 AM   #4
exitsfunnel
Member
 
Registered: May 2003
Distribution: RedHat
Posts: 60

Original Poster
Rep: Reputation: 15
Thanks itsme! That solved the problem.
 
  


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
Trouble: mounting, linking, permissions. crackery Linux - Newbie 2 01-28-2005 09:03 PM
Linking whole archive into shared lib mj_ Programming 0 07-23-2004 06:00 AM
Trouble linking with libmqueue on Linux 2.6.5 -mm illuminatedwax Programming 0 04-14-2004 07:55 AM
trouble linking with g++ WanglessOlasov Programming 5 12-28-2003 12:25 PM
importing an MS Outlook archive archive.pst alloydog Linux - Software 2 08-29-2003 03:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration