第一个makefile文件
时间:2007-06-12 来源:purple_lotuses
今天自己写了一个makefile文件,经过一番调试终于成功了,有一些小小的安慰:现将过程记录如下:
1. 首先源程序里不能有误,不然-c生成目标文件的时候就会报错
2. 所有源文件生成目标文件之后会使用-o生成可执行文件,这一文件名可随便写,后缀名也不作要求。
3. 在-上一步容易产生makefile:2: *** missing separator. Stop. 这一错误是由编译时无法识别分隔符引起的,可能会是因为在gcc命令前用空格代替了Tab引起的,在所有的gcc前都要使用Tab键隔开。还有那几个英文的:不能错了。当所有的这些都符合要求了,可能还会提示此错误,这就可能是因为makefile的命令里有空格或是其他的错误,可以单个的将命令运行一下就可以找到了:)
4. 所有的-o都生成后,可能还会报告Storage.o中找不到关于mysql的一些函数,这需要在生成可执行文件的命令里加上-lmysqlclient,这个的具体原因我也不太清楚。
5. 最后生成可执行文件cy,.#/cy 执行,报告error while loading shared libraries: libmysqlclient.so.15: cannot open shared object file: No such file or directory
解决方案如下:
The dynamic linker (ld.so) does not know it’s supposed to search for these shared libs in that directory. You have several options to resolve this:
- Add /usr/local/mysql/lib/mysql to you LD_LIBRARY_PATH environment variable
- copy the shared mysqlclient.so files into /usr/local/lib and run “ldconfig -v”
- Add the path /usr/local/mysql/lib/mysql to /etc/ld.so.conf
You can use “ldd /usr/local/mysql/bin/mysql” to verify that all shared libs are actually found.
我使用了如下命令后,再执行cy就可以了。
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/caoyan/myeye:/usr/local/mysql/lib/mysql
6. 以上就是整个过程,我的makefile文件如下:
cy:Common.o Entry.o Listen.o Parser.o Storage.o
gcc -Iinclude -I/usr/local/mysql/include/ -I/usr/local/include -L/usr/local/mysql/lib/mysql -lmysqlclient -L./ Common.o Entry.o Listen.o Parser.o Storage.o -LS -o cy
Common.o:Common.c config.h platform.h structs.h neyed.h
gcc -c Common.c -Iinclude -I/usr/local/mysql/include/ -I/usr/local/include -L/usr/local/mysql/lib/mysql -L./
Entry.o:Entry.c config.h neyed.h getopt.h
gcc -c Entry.c -Iinclude -I/usr/local/mysql/include/ -I/usr/local/include -L/usr/local/mysql/lib/mysql -L./
Listen.o:Listen.c config.h neyed.h
gcc -c Listen.c -Iinclude -I/usr/local/mysql/include/ -I/usr/local/include -L/usr/local/mysql/lib/mysql -L./
Parser.o:Parser.c config.h neyed.h
gcc -c Parser.c -Iinclude -I/usr/local/mysql/include/ -I/usr/local/include -L/usr/local/mysql/lib/mysql -L./
Storage.o:Storage.c config.h neyed.h
gcc -c Storage.c -Iinclude -I/usr/local/mysql/include/ -I/usr/local/include -L/usr/local/mysql/lib/mysql -L./
注:makefile中陈述关系时,只包含自己定义的头文件(""),系统的头文件(<>)不包含。