problem when making a library

Asked

Viewed 55 times

0

I made a code in a library and when I call her in the main it gives a mistake collect2.exe: error: Ld returned 1 Exit status follows the codes library. c

#include "biblioteca.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char *getPath(char *fullPath){
    int i,tam;
    char *recebe;

    tam = strlen(fullPath);
    recebe =(char*) malloc (tam * sizeof (char));

    for(i=tam;i>=0;i--){
        if(*(fullPath+i)=='/')
            break;
    }
    strncpy(recebe,fullPath,i);
    *(recebe+i)='\0';

    return recebe;
}

char *getFileName(char *fullPath){
    char *recebe;
    int i,tam;

    tam=strlen(fullPath);

    for(i=tam;i>=0;i--){
        if(*(fullPath+i)=='/')
            break;
        tam--;
    }
    i=strlen(fullPath)-i-1;
    recebe=(char*)malloc (i * sizeof (char));
    strncpy(recebe,&fullPath[tam+1],i);
    *(recebe+i)='\0';

    return recebe;
}

char *getSuffix(char *name){
    char *sufixo;
    int i,tam,onde;

    tam=strlen(name);
    onde=tam;

    for(i=tam;i>=0;i--){
        if(*(name+i)=='.'){
            break;
        }
        onde--;
    }
    i=tam+1-i;
    sufixo =(char*) malloc (i * sizeof (char));

    strncpy(sufixo,&name[tam+1],i);
    *(sufixo+i)='\0';

    return sufixo;
}

int hasSlash(char *path){
    int verdade=0,tam;
    tam=strlen(path)-1;
    if(*(path+tam)=='/')
        verdade=1;
    return verdade;

}

char *concatPathFile(char *path, char *fileName){
    int tamPath,tamFile;
    char *concatena;

    tamPath = strlen(path);
    tamFile = strlen(fileName);

    concatena =(char*) malloc ((tamPath+tamFile-1) * sizeof (char));
    if (hasSlash(path) == 1){

        strcpy(concatena,path);
        strcat(concatena,fileName);
    }
    else{

        strcpy(concatena,path);
        strcat(concatena,"/");
        strcat(concatena,fileName);
    }

    return concatena;
}

library. h

#ifndef BIBLIOTECA_H_INCLUDED
#define BIBLIOTECA_H_INCLUDED

char *getPath(char *fullPath);
/*
Dado o nome completamente qualificado de um arquivo,
retorna o caminho, sem ‘/’ no final. 
*/

char *getFileName(char *fullPath);
/*
Retorna o nome do arquivo, incluindo a extensão
*/

char *getSuffix(char *name);
/*
Retorna a extensão do arquivo. 
O nome do arquivo pode ser precedido ou não por um caminho absoluto ou relativo.
*/

int hasSlash(char *path);
/*
Retorna verdadeiro se o caminho “path” termina em ‘/’; falso, caso contrário.
*/

char *concatPathFile(char *path, char *fileName);
/*
Qualifica o arquivo de nome fileName com o caminho path. Ou seja, concatena o segundo (fileName) após o primeiro (path). 
Caso path não termine com /, acrescenta esta barra entre o path e o fileName.
*/

#endif

main. c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "biblioteca.h"

void print(char *texto){
    printf("%s",texto);
}

int main(){
    char *caminho1="a/b/c";
    

    print(getFileName(caminho1));
    return 0;
}

in the case the main was more to test

  • Ask before, did you generate . o, for example lib before compilation? second is gcc? third this print() of yours, is part of your lib or would already be an error in the syntax?

  • yes gerei, n know if it is gcc (I used code Blocks), print is a function in main (I wanted to see if it would work)

  • In the case of gcc, which commands should I use?

  • For codeblock, in Windows, I think it uses Mingw, so I believe the commands will work. Try it there: ... hold on I’m going to post down here didn’t work out so well

  • Testing your code here, followed the instructions below, on Linux with GCC compiled correctly. So error in the compilation code does not have at all, it is more than a matter of linkage. If these flags don’t work, you’d have to see what codeblock needs to generate a static link.. sometimes you can change one term or another, but the problems are here.

1 answer

0


If you can get through through through through the end you go through

gcc(se for gcc ai ou outro comando) -c biblioteca.o biblioteca.c

It will generate a binary and with it you will compile with main to link correctly.

gcc -o nome_da_saida main.c biblioteca.o

This will generate a unique executable. There are other ways to compile, but this one works.

If you are using LLVM I believe that will be the same flags, for Microsoft Compiler I can not imagine, since I do not use Windows.

If for some reason you can’t be sure of the commands passed in the codeblock options, but the ideal is that you do the commands in your hands or generate a make or ninja file or who using something like cmake(I don’t like it, but there it is), Meson. For few lines sometimes does not pay, but for many command lines will need.

For the print function, as it does not modify much it would be better to use a function macro or pass printf straight, it will be better for you to debbugar if it is the printf itself.

#define print(x) \
    printf("%s\n", x)

See if it happens, if it doesn’t, send messages that we try another solution.

  • This appears qnd use "-c library. the library. c" it returns that message "gcc.exe: Warning: library. o: Linker input file unused because linking not done" "?

  • Name of the output is the name of the final file. For example and Voce give no name using -o == output on linux will come out with the old format name a.out as the default runtime name. in Windows may come out something.exe, but you can set the name of the final run.

  • And about Inker, he’s saying the exit wasn’t used. It may be that it has some rule in the compiler that looks for the name ofmrato of the object file that will be linked later. When executing the last gcc command you need to have the correct names for the compiler to find the file. Try to make the complete composition via terminal using this gcc.exe in the directory you are generating the compilation. Then if it works out try to configure codeblock, at least then you already get how it works a little more from the gcc compilation step.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.