0

Hello

Outline of the problem:
Files:
1. a.cpp, a.h, atest.cpp
2. b.cpp, b.h

- file a.cpp is compiled to liba.so
- file b.cpp is compiled to libb.so
- function af01 defined in a.cpp calls the function bf01 defined in b.cpp
- file atest.cpp is compiled to executable program, which calls the af01 function therefore it calls indirectly the bf01 function

During atest.cpp compilation a compiler claims that:
mormegil@devel-1:/vol/cuma/sandbox$ g++ -Wall -L/vol/cuma/sandbox atest.cpp -la -o atest
/vol/cuma/sandbox/liba.so: undefined reference to `bf01(int)'

What is the way of correct compilation of liba.so ?

Up until now I did it in the following manner :

mormegil@devel-1:/vol/cuma/sandbox$ g++ -Wall -fPIC -c a.cpp
mormegil@devel-1:/vol/cuma/sandbox$ g++ -shared -W1,-soname,liba.so.1 -o liba.so.1.0 a.o -L/vol/cuma/sandbox/ -lb
mormegil@devel-1:/vol/cuma/sandbox$ ln -sf /vol/cuma/sandbox/liba.so.1.0 /vol/cuma/sandbox/liba.so
mormegil@devel-1:/vol/cuma/sandbox$ ln -sf /vol/cuma/sandbox/liba.so.1.0 /vol/cuma/sandbox/liba.so.1
and finally :
mormegil@devel-1:/vol/cuma/sandbox$ g++ -Wall -L/vol/cuma/sandbox atest.cpp -la -o atest

If the function af01 doesn't call the bf01 from libb.so the atest compiles and works well.

Best regards

Cuma

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by ctyszkiewicz
0

You have to also add the libb.so to the compilation of atest. Use the following command line to compile atest:
g++ -Wall -L/vol/cuma/sandbox atest.cpp -la -lb -o atest

Notice the addition of -lb

0

Dear Mike

Unfortunately it doesn't work giving the same error:
/vol/cuma/sandbox/liba.so: undefined reference to `bf01(int)'

In the first, I have built the libb.so:

g++ -Wall -fPIC -c b.cpp
g++ -shared -W1,-soname,libb.so.1 -o libb.so.1.0 b.o
ln -sf /vol/cuma/sandbox/libb.so.1.0 /vol/cuma/sandbox/libb.so
ln -sf /vol/cuma/sandbox/libb.so.1.0 /vol/cuma/sandbox/libb.so.1

In the second step, the liba.so

g++ -Wall -fPIC -c a.cpp
g++ -shared -W1,-soname,liba.so.1 -o liba.so.1.0 a.o -L/vol/cuma/sandbox -lb
ln -sf /vol/cuma/sandbox/liba.so.1.0 /vol/cuma/sandbox/liba.so
ln -sf /vol/cuma/sandbox/liba.so.1.0 /vol/cuma/sandbox/liba.so.1

At the end, the atest
g++ -Wall -L/vol/cuma/sandbox atest.cpp -la -lb -o atest

Maybe something wrong is with the sources:
____________________________
/* File: b.h */
#include <sys/types.h>
#include <string>

using namespace std;

// declaration
string bf01(int);

_____________________________
/* File: b.cpp */
/* libb source */

#include <iostream>
#include <sys/types.h>
#include <string>
#include "b.h"

using namespace std;

// definicja funkcj
string bf01(unsigned int a) {
if (a<3) {
a=3;
}
string s = "x";
string t = "y";
unsigned int i;
for(i=1;i<a;++i) {
s.append(t);
}
return s;
}
______________________________
/* File: a.h */
#include <sys/types.h>
#include <string>
#include "b.h"

using namespace std;

// declaration funkcji
int af01(string);

______________________________
/* File: a.cpp */
/* liba source */

#include <iostream>
#include <sys/types.h>
#include <string>
#include "a.h"
#include "b.h"

using namespace std;

int af01(string str1) {

int i=3;
string b;
b = bf01(i); // call to libb
b = b + str1;
return 0;
}
________________________________
/* File: atest.cpp */
#include <stdio.h>
#include "a.h"

using namespace std;

int main(int argc, char **argv) {

if (argc != 2) {
char err_msg[] = "error";
puts(err_msg);
return 1;
}
else {
af01(argv[1]); // call to liba
return 0;
}
}

0

Why don't you just put both *.cpp files in the same *.so?

In the task I have been given I have a complex shared library and I develop my own small shared library which uses the function of the first one. I can't compile program which use my library because the I have unresolved references to the functions of the first mentioned library.
So I have built two very simple libraries in order to learn the problem.

Cuma

0

Your prototype for bf01:

string bf01(int);

Does not match your definition:

string bf01(unsigned int a) {

Matching the two should solve your problem.

0

Your prototype for bf01:

string bf01(int);

Does not match your definition:

string bf01(unsigned int a) {

Matching the two should solve your problem.

Hello

Thank you very much! After correction the header file b.h I have found that the proper chain of compilation is:
g++ -Wall -fPIC -c a.cpp
g++ -shared -Wl,-soname,liba.so.1 -L/vol/cuma/sandbox -lb -o liba.so.1.0 a.o
ln -sf /vol/cuma/sandbox/liba.so.1.0 /vol/cuma/sandbox/liba.so
ln -sf /vol/cuma/sandbox/liba.so.1.0 /vol/cuma/sandbox/liba.so.1

g++ -Wall -L/vol/cuma/sandbox -la atest.cpp -o atest

Best regards

Cuma

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.