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?
– Talles
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)
– KSM
In the case of gcc, which commands should I use?
– KSM
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
– Talles
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.
– Talles