gcc
参数
-L 库路径 ./lib
-l(小写L) 库名称
-I(大写I) 头文件路径 ./include
编译:main.c test.h test.cpp
test.h
#ifndef TEST_H
#define TEST_H
void test();
#endif
test.cpp
#include<stdio.h>
void test()
{
printf("test !!!\n");
}
main.c
#include <stdio.h>
#include "test.h"
int main()
{
test();
}
生成动态库
gcc -fPIC -shared -o libmytest.so test.c
file libmytest.so // 查看属性
调用动态库
可以删掉test.c
方法1:添加lib
cp libmytest.so /lib
gcc main.c -o main -l mytest - L . (可不加 -L .)
./main
方法2:修改配置文件
cd /etc/ld.so.conf
vim : 添加动态库路径,如
/home/admin/gcctest/
生效配置文件:
ldconfig /etc/ld.so.conf
编译:
gcc main.c -o main -l mytest - L .
./main
Makefile
编译 main.c print.c Makefile
Makefile
CXX = g++
TARGET = hello
OBJ = main.o print.o
$(TARGET) : $(OBJ)
$(CXX) -o $(TARGET) $(OBJ)
main.o : main.cpp
$(CXX) -c main.cpp
print.o : print.cpp
$(CXX) -c print.cpp
执行:
make
./hello