qq_16231679
qq_16231679
采纳率100%
2018-05-29 03:08 阅读 2.2k

如何将子目录.o合并单个.o,每个子目录.o合成可执行文件

5

最近在学习Linux下的编程,打算自己学习一下Makefile的编写。太复杂的Makefile看不懂,想简单了解一下Makefile的编写。想学习一下多级目录的多个Makefile怎么写,以及多目录的Makefile文件的调用。
现在有一个想法,是否可以将每个子目录的.o合并成一个.o,又在上级目录中将这些.o生成单个.o,一级一级往上,在顶层目录下生成可执行文件。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    github_37157365 木兰宿莽 2018-05-29 04:59

    不可以。
    比方我有两个.c文件add.c和min.c

    add.c

     int add(int x, int y)
    {
    return x + y;
    }
    

    min.c

     int min(int x, int y)
    {
    return x - y;
    }
    

    分别编译
    gcc -c add.c -o add.o
    gcc -c min.c -o min.o

    如果你想直接合并两个.o
    gcc add.o min.o -o tar.o
    会报类似如下的错误:
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
    /usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':

    不过有另外的方法,就是把.o文件生成.a或者.so文件。

    gcc -shared -fPIC add.o min.o -o tar.so

    这样是可以的。

    点赞 1 评论 复制链接分享
  • daxiang520 aaronddup 2018-05-29 07:10

    在每个目录下生成静态库就可以了
    cd 目录1 ; ar crv libmylib1.a a.o b.o
    cd 目录2; ar crv libmylib2.a c.o d.o

    在上一级目录,先把libmylib1.a libmylib1.a 解开,
    ar x 目录1/libmylib1.a
    ar x 目录2/libmylib2.a

    再合并成一个大的.a
    ar crv libbiglib.a *.o

    点赞 1 评论 复制链接分享

相关推荐