【问题标题】:undefined reference to `test' when linking shared object链接共享对象时对“测试”的未定义引用
【发布时间】:2012-11-26 04:41:20
【问题描述】:

我正在学习如何在 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

哈拉.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;
}

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

【问题讨论】:

    标签: c linker makefile shared-libraries


    【解决方案1】:

    链接glenn.c时应添加-lhaha

    gcc glenn.c -L. -lhala -o test
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 谢谢。这对我有用。我有另一个问题。如果我使用测试: glenn.c libhala.so gcc glenn.c libhala.so -o test 这也适用于我。它们是否相同?
      • @TrafalgarLaw,更喜欢的方式是使用-lhala
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 2013-06-21
      • 2016-03-02
      • 2017-03-20
      • 2019-02-12
      • 2019-10-31
      • 2021-10-25
      相关资源
      最近更新 更多