文件目录:
code\main.c
#include <stdio.h>
#include "hello.h"
int main(){
const char* str = "hello";
printf( "%d\n", getLength( str ));
}
code\hello\hello.c
#include <string.h>
int getLength( const char* str ){
return strlen( str );
}
code\hello\hello.h
int getLength( const char* str );
GCC 编译,连接:
静态库:
当前目录:code\hello\
gcc -c hello.c -o hello.o
ar cr libhello.a hello.o 生成静态库
当前目录:code\
方案1:gcc main.c -Ihello -Lhello -lhello -o main
方案2:gcc main.c -lhello -static-libgcc libhello.a -o main
动态库:
当前目录:code\hello\
gcc hello.c -fPIC -shared -o libhello.so 生成动态库
当前目录: code\
方案1:gcc hello.c -Ihello -Lhello -lhello -o main
(在linux下需要将libhello.so的目录加到LD_LIBRARY_PATH (如:export
LD_LIBRARY_PATH=$LD_BRARY_PATH:.))
方案2:用linux下dlopen等函数( 待解决 )
code\main.c
#include <stdio.h>
#include "hello.h"
int main(){
const char* str = "hello";
printf( "%d\n", getLength( str ));
}
code\hello\hello.c
#include <string.h>
int getLength( const char* str ){
return strlen( str );
}
code\hello\hello.h
int getLength( const char* str );
GCC 编译,连接:
静态库:
当前目录:code\hello\
gcc -c hello.c -o hello.o
ar cr libhello.a hello.o 生成静态库
当前目录:code\
方案1:gcc main.c -Ihello -Lhello -lhello -o main
方案2:gcc main.c -lhello -static-libgcc libhello.a -o main
动态库:
当前目录:code\hello\
gcc hello.c -fPIC -shared -o libhello.so 生成动态库
当前目录: code\
方案1:gcc hello.c -Ihello -Lhello -lhello -o main
(在linux下需要将libhello.so的目录加到LD_LIBRARY_PATH (如:export
LD_LIBRARY_PATH=$LD_BRARY_PATH:.))
方案2:用linux下dlopen等函数( 待解决 )