example of Makefile to compile a c source file

本文介绍了一个简单的Makefile示例,用于编译C语言源代码。该Makefile包括了目标文件的生成规则及清理命令。通过这个例子,读者可以了解如何设置依赖关系以确保代码修改后能正确重新编译。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

example of Makefile to compile a c source file

****************************************************************************************

modify_time: modify_time.o
        gcc -o modify_time modify_time.o
modify_time.o: modify_time.c      # the prerequisite 'modify_time.c' CAN'T be omited!!!!! otherwise although modify_time.c modified, it will still prompt 'target is up-to-date' and the modification can't be compiled.
        gcc -c modify_time.c
clean:
        rm -rf *.o modify_time

****************************************************************************************

the below two lines in this Makefile can be omited:

modification can't be compiled.
        gcc -c modify_time.c

在‘跟我一起写Makefile’中有一个Makefile编译多个c文件的例子。

Resolving the 'multiple definition of yylloc' Error 1 2 3 The multiple definition of 'yylloc' error occurs during the compilation process, typically when building the Linux kernel or related projects. This error indicates that the yylloc symbol is defined in multiple places, causing a conflict. Example /usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x10): multiple definition of 'yylloc'; scripts/dtc/dtc-lexer.lex.o:(.bss+0x0): first defined here collect2: error: ld returned 1 exit status Solution 1: Modify Source Code One common solution is to modify the source code to declare yylloc as an external variable 1 . Steps: Open the file scripts/dtc/dtc-lexer.lex.c. Find the line YYLTYPE yylloc;. Change it to extern YYLTYPE yylloc;. Example: // Original line YYLTYPE yylloc; // Modified line extern YYLTYPE yylloc; Solution 2: Use an Older GCC Version The error can also be caused by using a newer version of GCC (e.g., GCC 10 or later). Downgrading to an older version like GCC 9 can resolve the issue 2 . Steps: Install GCC 9. Set it as the default compiler. Example: sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9 Solution 3: Clean and Rebuild Sometimes, old object files can cause this issue. Cleaning the build directory and rebuilding can help 3 . Steps: Clean the build directory. Rebuild the project. Example: make clean make By following these steps, you can resolve the multiple definition of 'yylloc' error and successfully compile your project. Learn more 1 github.com 2 blog.youkuaiyun.com 3 github.com
04-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值