从来没系统的看过makefile文档,平时属于复制模板,用完即忘,下午尝试按自己的理解写一个最简单的makefile,含2个.c文件,1个.h文件,费了个把小时,参考别人的文章才弄出来,特记录。

main.c:

 #include <stdio.h>
#include "command.h" int main(int argc, const char *argv[])
{
printf("run in main\n");
command();
return ;
}

commad.c:

 #include <stdio.h>
#include "command.h" void command(void)
{
printf("run in command\n");
}

commad.h:

 #ifndef __COMMAND_H__
#define __COMMAND_H__ void command(void); #endif

makefile:

 objects = main.o command.o

 main:$(objects)
gcc $(objects) -o main main.o:main.c command.h
gcc main.c -o main.o command.o:command.c command.h
gcc command.c -o command.o .PHONY:clean
clean:
rm main $(objects)

make之后,报错:

  gcc main.c -o main.o
  /tmp/ccTBTS4t.o:在函数‘main’中:
  main.c:(.text+0x16):对‘command’未定义的引用
  collect2: error: ld returned 1 exit status
  make: *** [main.o] 错误 1
于是开始了我的第一个错误理解,我的makefile出错在6-7行,执行生成目标main.o时,我看到错误,认为是我的目标依赖应该要包含commad.o,因为我的main.c调用了command.c里面的函数。于是我为main.o添加依赖commad.o,

 objects = main.o command.o

 main:$(objects)
cc $(objects) -o main main.o:main.c command.o command.h
gcc main.c command.o -o main.o command.o:command.c command.h
gcc command.c -o command.o .PHONY:clean
clean:
rm main $(objects)

看到6-7行,觉得超不对劲,姑且不管,再次make,由于依赖关系,推导执行到command.o:

  gcc command.c -o command.o
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
  /usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o:在函数‘_start’中:
  (.text+0x18):对‘main’未定义的引用
  collect2: error: ld returned 1 exit status
  make: *** [command.o] 错误 1

看到了/usr/bin/ld,没错,就是进行了链接操作,也就是生成可执行文件的最后一步,问题是我这里只不过是想把command.c生成目标文件command.o。于是怀疑gcc  command.c -o command.o这个格式不正确。回忆下一个可执行文件的生成过程:预编译,汇编(对高级于汇编语言的才有),编译,链接。而我只想它停在编译后。将这几个步骤拆分:

预编译:

  gcc -E a.c -o a.i

汇编:

  gcc -S a.i -o a.s

编译:

  gcc -c a.s -o a.o

链接:

  gcc a.o -o a

  其实最后这个链接还可以细分,比如这里会将command.o main.o ,libc库(我用了printf),还有就是链接加载库,一起链接成可执行文件。

于是我的错误就漏了 “-c” ,补上:

 objects = main.o command.o

 main:$(objects)
cc $(objects) -o main main.o:main.c command.h
gcc -c main.c -o main.o command.o:command.c command.h
gcc -c command.c -o command.o .PHONY:clean
clean:
rm main $(objects)

main.c中调用了command.c里面的函数也并不需要将其作为main.o的依赖,因为main.o中调用command()函数会留一个符号表在main.o文件中,直到最后进行链接的时候才确定其具体值。

原始的2文件的makefile错误的更多相关文章

  1. 关于SQL Server 安装程序在运行 Windows Installer 文件时遇到错误

    前几日安装sql server2008r2 的时候碰到这个问题: 出现以下错误: SQL Server 安装程序在运行 Windows Installer 文件时遇到错误. Windows Insta ...

  2. php -l 检查文件是否语法错误

    有时候在进行网页开发的时候,后台文件的语法错误比较难检查出来,这时候使用php -l filename可对文件的语法进行检查.

  3. IIS7下swfupload上传大文件出现404错误

    要求上传附件大小限制在2G,原本以为可以轻松搞定.在编译模式下可以上传大文件,可是在IIS7下(自己架的服务器),一上传大的文件就会出现 Http 404错误,偶尔有的文件还有IO. error错误. ...

  4. word2007在试图打开文件时遇到错误解决方法

    当您尝试在 Microsoft Office Word 2007 中打开 .docx 文件时,该文件打不开.此外,您还会收到以下错误消息: Word 在试图打开文件时遇到错误.请尝试下列方法:* 检查 ...

  5. sa命令从/var/account/pacct原始记账数据文件读取信息并汇总

    sa命令从/var/account/pacct原始记账数据文件读取信息并汇总

  6. 内核编程实例,多文件的Makefile

    内核编程实例,多文件的Makefile 经典的hello word测试 ////# cat hello.c #include <linux/module.h> #include <l ...

  7. 关于SSIS批量抽取Excel文件报0x80004005错误的解决办法

    原文:关于SSIS批量抽取Excel文件报0x80004005错误的解决办法 标题: Microsoft Visual Studio ------------------------------ Pa ...

  8. office 2013幻灯片中插入SmartArt图形时出现错误下列一个或多个文件由于包含错误而无法运行

    office 2013幻灯片中插入SmartArt图形时出现错误下列一个或多个文件由于包含错误而无法运行 系统:win8 64位 PowerPoint2013 64位 在幻灯片中插入SmartArt图 ...

  9. 运行python “没有那个文件或目录3” 或 “/usr/local/bin/python3^M: bad interpreter: 没有那个文件或目录” 错误

    原因 如果使用的是#!/usr/local/bin/python3这种方式,就会出现 “/usr/local/bin/python3^M: bad interpreter: 没有那个文件或目录” 错误 ...

随机推荐

  1. c# 正则表达式 匹配中括号&颜色过滤

    现在需要匹配 [color=#000000],以"[color"开头,以"[/color]"结束,中间字符数量不限制,最后返回所有匹配的下标. 代码如下: // ...

  2. Redis持久化-数据丢失及解决

    Redis的数据回写机制 Redis的数据回写机制分同步和异步两种, 同步回写即SAVE命令,主进程直接向磁盘回写数据.在数据大的情况下会导致系统假死很长时间,所以一般不是推荐的. 异步回写即BGSA ...

  3. C# LLSQL快速查询框架

    介绍一种新类型查询方法,类似linq,lambda语法,类似标准的sql使用习惯,支持匿名类型,泛型,目前支持mssql,mysql, 切换只需要DatabaseConfig.DatabaseType ...

  4. csharp: Export or Import excel using MyXls,Spire.Xls

    excel 2003 (效果不太理想) using System; using System.Collections.Generic; using System.ComponentModel; usi ...

  5. MySQL架构

    一.MySQL逻辑架构         第一层,即最上一层,所包含的服务并不是MySQL所独有的技术.它们都是服务于C/S程序或者是这些程序所需要的 :连接处理,身份验证,安全性等等.         ...

  6. [mysql] timestamp自动更新和初始化

    1.概述 在我们设计表的时候,考虑将行数据的创建时间和最后更新时间记录下来是很好的实践.尤其是可能需要做数据同步或者对数据新鲜度有要求的表.举些应用场景,更新距上次更新超过2小时的行数据,或者是将一个 ...

  7. 开启Windows Server 2008 R2上帝模式

    TAG标签: 摘要:这个“God Mode” 应该大部分的网友都听过了,只是在 Windows Server 2008 R2 上也支持此一功能.启用方式非常简单,在桌面新建一个文件夹,命名为: God ...

  8. 硬盘变成RAW的修复过程

    可能在不知道为什么的情况下,移动硬盘或者本地磁盘的每个分区变成了RAW格式.其在Win系统下的无损修复过程如下: 用“win”+“R”打开“运行”小窗口: 键入“CMD”: 键入命令“CHKDSK P ...

  9. basket.js 源码分析

    basket.js 源码分析 一.前言 basket.js 可以用来加载js脚本并且保存到 LocalStorage 上,使我们可以更加精准地控制缓存,即使是在 http 缓存过期之后也可以使用.因此 ...

  10. 高清HDMI编码器|上海视涛科技

    HDMI编码器(E300)简介 HDMI编码器(E300)是上海视涛科技出品的高性能HDMI+VGA编码产品.该HDMI+VGA编码器是上海视涛科技完全自主研发,并适用于VGA.DVI.HDMI等信号 ...