Why does this compile OK, but fail to build?

Discussion in 'C Programming' started by DFS, Jun 6, 2014.

  1. DFS

    DFS Guest

    #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) {
    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);

    DFS, Jun 6, 2014
    1. Advertisements

  2. 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.

    Barry Schwarz, Jun 6, 2014
    1. Advertisements

  3. DFS

    DFS Guest

    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:

    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!

    DFS, Jun 6, 2014
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.