Unexpected unresolved link error

I have written a library and a program that use it.
Test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <advmath.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main() {
	cout << "Testing with x" << endl; // prints !!!Hello World!!!
	MATH_CONTEXT_ADV context=adv_init();
	for(int i=0;i<5;i++){
		cout<<adv_rand(&context)<<endl;
	}
	return 0;
}


...And the header; advmath.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef ADVMATH_H_
#define ADVMATH_H_
#define ADV_MATH_MODE_DEGREE 0x0001
#define ADV_MATH_MODE_RADIAN (0x0001<<1)
typedef unsigned int MATH_MODE;
typedef struct _ADV_MATH_CXT{
	long long entropy;
	MATH_MODE Trimode;
}MATH_CONTEXT_ADV;
typedef struct _ADV_MATH_QUARD_RESULT{
	float x1,x2;
}MATH_QUARD_RESULT;
MATH_CONTEXT_ADV _cdecl adv_init();
void _cdecl adv_set_math_mode(MATH_CONTEXT_ADV* ctx,MATH_MODE m);
//basic functions
int _cdecl adv_rand(MATH_CONTEXT_ADV* ctx);
int _cdecl adv_rand_r(MATH_CONTEXT_ADV* ctx,int l,int u);
void _cdecl adv_srand(MATH_CONTEXT_ADV* ctx,int b);
void _cdecl adv_srand_sf(MATH_CONTEXT_ADV* ctx,int b,int c,int d,int e);
MATH_QUARD_RESULT _cdecl adv_computeFactor(MATH_CONTEXT_ADV* ctx,float a,float b,float c);
float _cdecl adv_tan(MATH_CONTEXT_ADV* ctx,float t);

#endif /* ADVMATH_H_ */ 

...... The implemention; math_base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdlib.h>
#include <math.h>
#include <advmath.h>

int _cdecl adv_rand(MATH_CONTEXT_ADV* ctx){
	int* datas=(int*)ctx->entropy;
	int a=datas[time(0)%2];
	a|=datas[time(0)%2];
	adv_srand(ctx,a);
	return a;
}
int _cdecl adv_rand_r(MATH_CONTEXT_ADV* ctx,int l,int u){
	int x=adv_rand(ctx)*1000;
	return l+(x%u);
}
void _cdecl adv_srand(MATH_CONTEXT_ADV* ctx,int b){
	adv_srand_sf(ctx,b,~b,b&(ctx->entropy),b|(ctx->entropy));
}
void _cdecl adv_srand_sf(MATH_CONTEXT_ADV* ctx,int b,int c,int d,int e){
	long long p=b&c&d&e;
	p=~p|(e)|(b&c)|d;
	p|=~e;
	p*=p;
	p/=2;
	int* r=(int*)ctx->entropy;
	r[0]=p&b;
	r[1]=p&b;
}
MATH_CONTEXT_ADV _cdecl adv_init(){
	MATH_CONTEXT_ADV local;
	local.Trimode=ADV_MATH_MODE_RADIAN;
	local.entropy=time(0);
	return local;
}
void _cdecl adv_set_math_mode(MATH_CONTEXT_ADV* ctx,MATH_MODE m){
	ctx->Trimode=m;
}

Here is the setting that I used to build the library
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
gcc -IC:\Users\Administrator\Desktop\Project\libadvmath\include -O0 -g3 -Wall -c -fmessage-length=0 -o impl\math_base.o ..\impl\math_base.c
..\impl\math_base.c: In function 'adv_rand':
..\impl\math_base.c:12:13: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  int* datas=(int*)ctx->entropy;
             ^
..\impl\math_base.c:13:2: warning: implicit declaration of function 'time' [-Wimplicit-function-declaration]
  int a=datas[time(0)%2];
  ^
..\impl\math_base.c: In function 'adv_srand_sf':
..\impl\math_base.c:31:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  int* r=(int*)ctx->entropy;
         ^
g++ -static-libgcc -static-libstdc++ -shared -Wl,-soname=libadvmath.so -Wl,--out-implib=advmath.lib -Wl,--output-def=advmath.def -o liblibadvmath.dll impl\math_base.o
Build complete for project libadvmath
Time consumed: 553  ms.  


When I attempt to build the program I got:
1
2
3
4
5
6
7
8
g++ -LC:\Users\Administrator\Desktop\Project\libadvmath\Debug -static-libgcc -static-libstdc++ -o Test.exe src\Test.o -ladvmath
src\Test.o: In function `main':
C:\Users\Administrator\Desktop\Project\Test\Debug/../src/Test.cpp:15: undefined reference to `adv_init()'
C:\Users\Administrator\Desktop\Project\Test\Debug/../src/Test.cpp:17: undefined reference to `adv_rand(_ADV_MATH_CXT*)'
collect2.exe: error: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 719  ms.  
 

And the DEF file that generated
1
2
3
4
5
6
7
8
EXPORTS
    adv_init @1
    adv_rand @2
    adv_rand_r @3
    adv_set_math_mode @4
    adv_srand @5
    adv_srand_sf @6

Why?
Last edited on
Hi,
After removing the file Test.cpp (Test.cpp and main.cpp are the same things), the program compiles fine.

What compiler are you using?
The first thing I noticed is you are building your library with the -shared flag and naming your .so "libadvmath.so" (I think at least, I have never seen the -soname flag before). But in your test project you are trying to link against -ladvmath. Though I am by no means even close to be ing a regular GCC's user so that might not even matter, but even if that doesn't matter it doesn't hurt to check that the name you are linking against matches the name of the actual library file produced.

Other then that, make sure you double and triple check everything. Stuff like where you library files are (Are they in the correct folder) and other common things like that, since usually these are the cause of 95% of linking errors.

Generally speaking linking errors are quite hard for others to help solve on the forums because there is just so many unknowns.
In your client code (main.cpp)
1
2
3
4
5
6
7
#ifdef __cplusplus 
extern "C"{
#endif
#include <advmath.h>
#ifdef __cplusplus 
}
#endif 
Because c++ allows function overload, it has to "decorate" the function name in order to differentiate the signatures (name mangling)

By the way, you ought to solve those warnings.


@Z e r e o: the -l flag is just a shortcut, it will strip the `lib' prefix and the `.so', `.a' suffix
and it would be another error if it can't find the library (something like `cannot find -lname')
Mingw GCC, host is is Win 7
@Zereo Why need to do that? Since I already place _cdecl as calling convention of all function that exported.
Last edited on
Topic archived. No new replies allowed.