1

我正在学习如何在 C 中链接共享对象的教程

这是我的制作文件

test: glenn.c libhala.so
    gcc glenn.c -L. -o test

libhala.so: hala.o
    gcc -shared hala.o -o libhala.so

hala.o: hala.c hala.h
    gcc -c -Wall -Werror -fpic hala.c

clean:
    rm *.o
    rm *.so
    rm test

哈拉.h

#ifndef HALA
#define HALA

extern void test(char*);
#endif

hala.c

#include "hala.h"
#include <stdio.h>

extern void test(char* s)
{

    printf("%s", s);
}

格伦.c

#include <stdio.h>
#include "hala.h"

int main()
{
    test("Hello There!");
    return 0;
}

这让我很充实。请帮帮我..

4

2 回答 2

2

您应该-lhaha在链接时添加glenn.c.

gcc glenn.c -L. -lhala -o test
于 2012-11-26T04:46:20.217 回答
1

-lhala编译时添加glenn.c,所以更新makefile为

test: glenn.c libhala.so
    gcc glenn.c -L. -lhala -o test
于 2012-11-26T04:46:39.137 回答