Solution to a code of a function without reference

0
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include "dummy2.h"

int exit_code=0;
void main(int argc, char *argv[]){
    int primero=atoi(argv[1]);
    int segundo=atoi(argv[2]);
    int fd[2];
    pipe(fd);
    fd[0]=dummy2_open( );
    for (int i=0; i<primero; ++i){
        int pid = fork();
        if(pid==0){
            execlp("./slave", "./slave", argv[2], (char *)NULL);
        }
    }
    int acum=0;
    while(waitpid(-1, NULL, 0)>0){
        int i=0;
        int rem=waitpid(-1, &exit_code, 0);

        if(rem==-1){
            printf("hi ha hagut un error al slave\n");
            exit(-1);
        }
        else{

            acum=rem+acum;
        }
    }
    dummy2_test(acum);

    sprintf(buffer, "Els fills han acabat\n");
    write(1, buffer, strlen(buffer));
    exit(0);
}

after compiling it, he replies with that

  

/tmp/ccA6uqSF.o: In function main': master.c:(.text+0x1d2): referencia a dummy2_open 'undefined   master.c :(. text + 0x2a0): reference to 'dummy2_test' without defining   collect2: error: ld returned 1 exit status   Makefile: 4: failure in the instructions for the 'master' objective   make: *** [master] Error 1

The content of dummy2.h

// Function headers for MASTER process
int  dummy2_open( );
int  dummy2_test( int );

// Function headers for SLAVE process
void dummy2_init( char buff[], int );
int  dummy2_comp( char buff[], int );
void dummy2_exit( );

and to compile it use

gcc master.c -o master -L -ldummy
    
asked by Pol Linger 04.01.2017 в 12:06
source

1 answer

1

If the code is in a different C file, you must compile them together or link the object code (if you compile it separately) or the function library.

Example compiling all the files together:

cc -o ejecutable master.c dummy2.c

With that, both will be compiled and will work.

If you already have it compiled (you have generated the object code with cc -c dummy.c ) and you just want to link it, then add the object code as follows:

cc -o ejecutable master.c dummy2.o

If you have generated (or have been provided) a library of functions (you can generate it yourself with the object code with ar crs libdummy2.a dummy.o ), then you should use:

cc -o ejecutable master.c libdummy2.a

Taking into account your last update, the exact way would be:

gcc master.c -o master -L. -ldummy2
    
answered by 04.01.2017 / 12:12
source