Why does this compile OK, but fail to build?


D

DFS

#include <stdio.h>
#include "mt64.h"

int mersenneTwister(void) {
int i;
unsigned long long init[4]={0x12345ULL, 0x23456ULL, 0x34567ULL,
0x45678ULL}, length=4;
init_by_array64(init, length);
printf("10 outputs of genrand64_int64()\n");
for (i=0; i<10; i++) {
printf("%20llu ", genrand64_int64());
if (i%5==4) printf("\n");
}
printf("\n10 outputs of genrand64_real2()\n");
for (i=0; i<10; i++) {
printf("%10.8f ", genrand64_real2());
if (i%5==4) printf("\n");
}
return 0;
}


int main(void) {
mersenneTwister();
return 0;
}



[[email protected] src]$ gcc -Wall mttest.c -o mttest
/tmp/cc93f16n.o: In function `mersenne_twister':
mttest.c:(.text+0x3f): undefined reference to `init_by_array64'
mttest.c:(.text+0x57): undefined reference to `genrand64_int64'
mttest.c:(.text+0xba): undefined reference to `genrand64_real2'
collect2: error: ld returned 1 exit status


mt64.h is in the same directory as the source file, of course.

It also fails to build if I remove the #include "mt64.h" and add in the
function declarations from the header file:

void init_by_array64(unsigned long long init_key[],
unsigned long long key_length);

unsigned long long genrand64_int64(void);

double genrand64_real2(void);



Wazzup?
 
Ad

Advertisements

B

Barry Schwarz

mt64.h tells the compiler how to interface with the functions you
call. It does not tell the linker where to find the actual
implementations of those functions. You need to add the appropriate
information for your system to your build process.

Since mt64.h probably contains more than just the function prototypes,
removing it and manually adding the prototypes is likely to be
incomplete. Provide the complete compiler error messages if you want
more help.

#include <stdio.h>
#include "mt64.h"

int mersenneTwister(void) {
int i;
unsigned long long init[4]={0x12345ULL, 0x23456ULL, 0x34567ULL,
0x45678ULL}, length=4;
init_by_array64(init, length);
printf("10 outputs of genrand64_int64()\n");
for (i=0; i<10; i++) {
printf("%20llu ", genrand64_int64());
if (i%5==4) printf("\n");
}
printf("\n10 outputs of genrand64_real2()\n");
for (i=0; i<10; i++) {
printf("%10.8f ", genrand64_real2());
if (i%5==4) printf("\n");
}
return 0;
}


int main(void) {
mersenneTwister();
return 0;
}



[[email protected] src]$ gcc -Wall mttest.c -o mttest
/tmp/cc93f16n.o: In function `mersenne_twister':
mttest.c:(.text+0x3f): undefined reference to `init_by_array64'
mttest.c:(.text+0x57): undefined reference to `genrand64_int64'
mttest.c:(.text+0xba): undefined reference to `genrand64_real2'
collect2: error: ld returned 1 exit status


mt64.h is in the same directory as the source file, of course.

It also fails to build if I remove the #include "mt64.h" and add in the
function declarations from the header file:

void init_by_array64(unsigned long long init_key[],
unsigned long long key_length);

unsigned long long genrand64_int64(void);

double genrand64_real2(void);



Wazzup?
 
Ad

Advertisements

D

DFS

mt64.h tells the compiler how to interface with the functions you
call. It does not tell the linker where to find the actual
implementations of those functions. You need to add the appropriate
information for your system to your build process.

Since mt64.h probably contains more than just the function prototypes,
removing it and manually adding the prototypes is likely to be
incomplete. Provide the complete compiler error messages if you want
more help.


Thanks Barry.

The compiler didn't throw any errors, and I showed the complete build
error messages down below.

But I figured it out; I had to add the .c file I got here:
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt64.html

wrong: gcc -Wall random.c -o random

right: gcc -Wall random.c mt19937-64.c -o random


I'm klutzy with C (and compilation/building) right now, so I appreciate
your help!




#include <stdio.h>
#include "mt64.h"

int mersenneTwister(void) {
int i;
unsigned long long init[4]={0x12345ULL, 0x23456ULL, 0x34567ULL,
0x45678ULL}, length=4;
init_by_array64(init, length);
printf("10 outputs of genrand64_int64()\n");
for (i=0; i<10; i++) {
printf("%20llu ", genrand64_int64());
if (i%5==4) printf("\n");
}
printf("\n10 outputs of genrand64_real2()\n");
for (i=0; i<10; i++) {
printf("%10.8f ", genrand64_real2());
if (i%5==4) printf("\n");
}
return 0;
}


int main(void) {
mersenneTwister();
return 0;
}



[[email protected] src]$ gcc -Wall mttest.c -o mttest
/tmp/cc93f16n.o: In function `mersenne_twister':
mttest.c:(.text+0x3f): undefined reference to `init_by_array64'
mttest.c:(.text+0x57): undefined reference to `genrand64_int64'
mttest.c:(.text+0xba): undefined reference to `genrand64_real2'
collect2: error: ld returned 1 exit status


mt64.h is in the same directory as the source file, of course.

It also fails to build if I remove the #include "mt64.h" and add in the
function declarations from the header file:

void init_by_array64(unsigned long long init_key[],
unsigned long long key_length);

unsigned long long genrand64_int64(void);

double genrand64_real2(void);



Wazzup?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top