Search code examples
macosgcclinkerstatic-librarieslinker-flags

while compiling a code using gcc on mac OS linker failed in linking custom made static library


here is the file structure for my project file in c-language :
first is a fullCourse folder
fullCourse folder contains three folders :
1. include
2. src
3. lib
4. test
in include folder :
it contains a header file : temp.h

code for temp.h is as follow :

#ifndef __$__temp_h
#define __$__temp_h 234
int yash();
#endif

in src folder :
It contains a source file : temp.c

code for temp.cc is as follow :

#ifndef __$__temp_c
#define __$__temp_c 234
int yash()
{
return 22;
}
#endif

then staying in same folder, created .o file as follow :

gcc -I ../include -c temp.c

next step was moving this temp.o file to lib folder as follow :

mv temp.o ../lib

than staying in lib folder created a archived (or library) file as follow :

ar rcs libtmds.a temp.o

than in test folder wrote a source code to test library(tempTest.c)
code for tempTest.c is as follow :

#include<stdio.h>
#include<temp.h>
int main()
{
int w;
w=yash();
printf("%d\n",w);
return 0;
}

than staying in test folder tried to compile it as follow :

gcc -static -I ../include tempTest.c -L ../lib -ltmds -o tempTest.exe

but code didn't compiled, showing following error :

ld: library not found for -lcrt0.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

please help me resolve this problem.


Solution

  • -static is in error here. To link to your static lib just plain link to it without any -static flag, which is for kernel compilation.