{"id":2551,"date":"2019-06-27T01:40:00","date_gmt":"2019-06-26T17:40:00","guid":{"rendered":"http:\/\/www.51cos.com\/?p=2551"},"modified":"2019-06-27T01:40:00","modified_gmt":"2019-06-26T17:40:00","slug":"makefile%e5%85%a5%e9%97%a8%e4%b8%8e%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"http:\/\/www.51cos.com\/?p=2551","title":{"rendered":"Makefile\u5165\u95e8\u4e0e\u5b9e\u4f8b"},"content":{"rendered":"<p>main.cpp\u6e90\u6587\u4ef6\uff1a\u5305\u542b\u5165\u53e3\u51fd\u6570 int main()\uff0c\u8be5\u6e90\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e86\u201cfunc.h\u201d\u5934\u6587\u4ef6\uff0c\u5728\u5165\u53e3\u51fd\u6570\u4e2d\u8c03\u7528func()\u51fd\u6570\u3002<br \/>\nfunc\u51fd\u6570\u4e2d\u5728func.cpp\u4e2d\u5b9a\u4e49\uff0c\u5728func.h\u4e2d\u539f\u578b\u58f0\u660e\u3002<\/p>\n<p>\u7f16\u5199Makefile\u6587\u4ef6<br \/>\n1\uff09\u5b9a\u4e49\u53d8\u91cf<br \/>\n\u9996\u5148\u5b9a\u4e49SOURCE\uff0cOBJS\u548cTARGET\u53d8\u91cf\uff0c\u7528\u4e8e\u6307\u4ee3\u6211\u4eec\u9879\u76ee\u4e2d\u7684\u6e90\u6587\u4ef6\u3001\u76ee\u6807\u6587\u4ef6\u548c\u53ef\u6267\u884c\u6587\u4ef6\u3002<br \/>\n2) \u8bbe\u7f6e\u7f16\u8bd1\u53c2\u6570<br \/>\nCC\uff1a\u914d\u7f6e\u7f16\u8bd1\u5668\u4e3ag++\uff0c<br \/>\nLIBS\uff1a\u9700\u8981\u8c03\u7528\u7684\u94fe\u63a5\u5e93\uff08-l\u5f00\u5934\uff0c\u53bb\u6389lib\u548c.so\u3002\u4f8b\uff1a\u5bf9 libopencv_core.so\u94fe\u63a5\u5e93\u7684\u8c03\u7528\u8981\u5199\u4f5c\uff1a-lopencv_core\uff09\uff0c<br \/>\nLDFLAGS\uff1a\u94fe\u63a5\u5e93\u7684\u8def\u5f84\uff08-L\u5f00\u5934\uff09\uff0c<br \/>\nINCLUDE\uff1a\u5934\u6587\u4ef6\u7684\u8def\u5f84\u3002<br \/>\n3\uff09\u94fe\u63a5\u751f\u6210<br \/>\n\u6b64\u6b65\u9aa4\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff08ELF\uff09\uff0c\u94fe\u63a5\u9700\u8981\u7528\u5230\u76ee\u6807\u6587\u4ef6\uff0c\u7531\u4e0b\u4e00\u6b65\u4ea7\u751f<br \/>\n4\uff09\u7f16\u8bd1<br \/>\n\u6b64\u6b65\u9aa4\u751f\u6210\u76ee\u6807\u6587\u4ef6\uff08.o\uff09<br \/>\n5\uff09\u6e05\u7406<br \/>\n\u6b64\u6b65\u9aa4\u6e05\u7406\u53ef\u6267\u884c\u6587\u4ef6\u548c\u6240\u6709\u7684\u76ee\u6807\u6587\u4ef6<\/p>\n<p> #######################<br \/>\n# Makefile<br \/>\n#######################<br \/>\n# source object target<br \/>\nSOURCE := main.cpp func.cpp<br \/>\nOBJS   := main.o func.o<br \/>\nTARGET := main<\/p>\n<p># compile and lib parameter<br \/>\nCC      := g++<br \/>\nLIBS    :=<br \/>\nLDFLAGS := -L.<br \/>\nDEFINES :=<br \/>\nINCLUDE := -I.<br \/>\nCFLAGS  :=<br \/>\nCXXFLAGS:= <\/p>\n<p># link<br \/>\n#<span class=\"katex math inline\">(TARGET):<\/span>(OBJS)<br \/>\n    <span class=\"katex math inline\">(CC) -o<\/span>@ <span class=\"katex math inline\">^<\/p>\n<p># compile<br \/>\n#<\/span>(OBJS):<span class=\"katex math inline\">(SOURCE)<\/span>(CC) -c main.cpp -o main.o<br \/>\n    <span class=\"katex math inline\">(CC) -c func.cpp -o func.o<\/p>\n<p># clean<br \/>\nclean:<br \/>\n    rm -fr *.o<br \/>\n    rm -fr<\/span>(TARGET)<\/p>\n<p>#\u5c06\u4e0a\u8ff0Makefile\u4e2d\u7684\uff1a<br \/>\n# link<br \/>\n#<span class=\"katex math inline\">(TARGET):<\/span>(OBJS)<br \/>\n    <span class=\"katex math inline\">(CC) -o<\/span>@ <span class=\"katex math inline\">^<\/p>\n<p># compile<br \/>\n#<\/span>(OBJS):<span class=\"katex math inline\">(SOURCE)<\/span>(CC) -c main.cpp -o main.o<br \/>\n    <span class=\"katex math inline\">(CC) -c func.cpp -o func.o<\/p>\n<p>#\u4e5f\u53ef\u4ee5\u4fee\u6539\u4e3a\uff1a<br \/>\nall:<\/span>(CC) -o <span class=\"katex math inline\">(TARGET)<\/span>(SOURCE)<\/p>\n<p>\u5c06func.cpp\u6e90\u6587\u4ef6\u5236\u4f5c\u6210\u52a8\u6001\u94fe\u63a5\u5e93libfunc.so\uff0c\u7136\u540e\u8c03\u7528\u8be5\u52a8\u6001\u5e93\u5bf9main.cpp\u8fdb\u884c\u7f16\u8bd1\u94fe\u63a5\u3002<\/p>\n<p> #######################<br \/>\n# Makefile \u52a8\u6001\u94fe\u63a5\u5e93\u7684\u751f\u6210<br \/>\n#######################<\/p>\n<p># compile and lib parameter<br \/>\nCC      := g++<br \/>\nLIBS    :=<br \/>\nLDFLAGS :=<br \/>\nDEFINES :=<br \/>\nINCLUDE := -I.<br \/>\nCFLAGS  :=<br \/>\nCXXFLAGS:= <\/p>\n<p># link parameter<br \/>\nLIB := libfunc.so<\/p>\n<p>#link<br \/>\n<span class=\"katex math inline\">(LIB):func.o<\/span>(CC) -shared -o -fPIC -o <span class=\"katex math inline\">@<\/span>^<br \/>\n#compile<br \/>\nfunc.o:func.cpp<br \/>\n    <span class=\"katex math inline\">(CC) -c -fPIC<\/span>^ -o <span class=\"katex math inline\">@ <\/p>\n<p># clean<br \/>\nclean:<br \/>\n    rm -fr *.o<\/p>\n<p>\u6267\u884cmake\u547d\u4ee4\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u5f53\u524d\u76ee\u5f55\u751f\u6210libfunc.so\u7684\u52a8\u6001\u94fe\u63a5\u5e93\u4e86\u3002<br \/>\n\u6ce8\u610f\uff1a\u52a8\u6001\u94fe\u63a5\u5e93\u5fc5\u987b\u201c\u4ee5lib\u5f00\u5934\uff0c\u4ee5.so\u7ed3\u5c3e\u201d\u3002<br \/>\n\u6ce8\uff1a\u7f3a\u5c11 -fPIC\u9519\u8bef<\/p>\n<p>\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u8c03\u7528\uff1a\u5982\u679c\u5e76\u6ca1\u6709\u63d0\u4f9bfunc.cpp\u548cfunc.o\uff0c\u53ea\u662f\u63d0\u4f9b\u4e86libfunc.so\u8fd9\u4e2a\u94fe\u63a5\u5e93\uff0c\u6211\u4eec\u5982\u4f55\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u5462\uff1f<\/p>\n<p>Makefile\u5982\u4e0b\uff1a<br \/>\n1\uff09\u7f16\u8bd1\u7684\u65f6\u5019\u9700\u8981\u901a\u8fc7INCLUDE\u6307\u660e\u5934\u6587\u4ef6\u7684\u8def\u5f84<br \/>\n2\uff09\u94fe\u63a5\u7684\u65f6\u5019\u9700\u8981\u901a\u8fc7LDFLAGS \u548c LIBS\u6307\u660e\u52a8\u6001\u5e93\u7684\u8def\u5f84\u548c\u540d\u79f0\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6307\u660e\u52a8\u6001\u5e93\u540d\u79f0\u65f6\u9700\u8981\u201c\u6390\u5934\u53bb\u5c3e\u201d\uff0c\u4f8b\uff1a\u6211\u4eec\u9700\u8981\u7528\u5230 libfunc.so\u5e93\uff0cLIBS\u5fc5\u987b\u5b9a\u4e49\u4e3a -lfunc \u3002<br \/>\n3\uff09\u6267\u884c\u7684\u65f6\u5019\uff0c\u9700\u8981\u628alibfunc.so\u52a8\u6001\u5e93\u62f7\u8d1d\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u5305\u542b\u7684\u8def\u5f84\u4e0b\uff08\u6bd4\u5982\/lib\u6216\/usr\/lib\uff09\uff0c\u8fd9\u6837\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u624d\u80fd\u8c03\u7528\u5230\u52a8\u6001\u5e93\u3002<\/p>\n<p> #######################<br \/>\n# Makefile<br \/>\n#######################<\/p>\n<p># target<br \/>\nTARGET := main<\/p>\n<p># compile and lib parameter<br \/>\nCC      := g++<br \/>\nLDFLAGS := -L\/path\/to\/libfunc.so<br \/>\nLIBS    := -lfunc<br \/>\nDEFINES :=<br \/>\nINCLUDE := -I\/path\/to\/func.h<br \/>\nCFLAGS  :=<br \/>\nCXXFLAGS:= <\/p>\n<p># link<\/span>(TARGET):main.o<br \/>\n    <span class=\"katex math inline\">(CC) -o<\/span>@ <span class=\"katex math inline\">^<\/span>(LDFLAGS) <span class=\"katex math inline\">(LI<\/p>\n<p>#compile<br \/>\nmain.o:main.cpp<\/span>(CC) <span class=\"katex math inline\">(INCLUDE) -c<\/span>^crm -fr <span class=\"katex math inline\">(TARGET)<\/p>\n<p>clean:<br \/>\n    rm -fr *.o<br \/>\n    rm -fr<\/span>(TARGET)<\/p>\n<p>\u9759\u6001\u94fe\u63a5\u5e93\u7684\u751f\u6210\u3002\u5c06func.cpp\u6e90\u6587\u4ef6\u5236\u4f5c\u6210\u9759\u6001\u94fe\u63a5\u5e93func.a\uff0c\u7136\u540e\u8c03\u7528\u8be5\u9759\u6001\u5e93\u5bf9main.cpp\u8fdb\u884c\u7f16\u8bd1\u94fe\u63a5\u3002\u6267\u884cmake\u547d\u4ee4\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u5f53\u524d\u76ee\u5f55\u751f\u6210func.a\u7684\u9759\u6001\u94fe\u63a5\u5e93\u4e86<\/p>\n<p> #######################<br \/>\n# Makefile<br \/>\n#######################<\/p>\n<p># compile and lib parameter<br \/>\nCC      := g++<br \/>\nLIBS    :=<br \/>\nLDFLAGS :=<br \/>\nDEFINES :=<br \/>\nINCLUDE := -I.<br \/>\nCFLAGS  :=<br \/>\nCXXFLAGS:= <\/p>\n<p># link parameter<br \/>\nAR  := ar<br \/>\nLIB := func.a<\/p>\n<p>#link<br \/>\n<span class=\"katex math inline\">(LIB):func.o<\/span>(AR) -r <span class=\"katex math inline\">@<\/span>^<br \/>\n#compile<br \/>\nfunc.o:func.cpp<br \/>\n    <span class=\"katex math inline\">(CC) -c<\/span>^ -o <span class=\"katex math inline\">@ <\/p>\n<p># clean<br \/>\nclean:<br \/>\n    rm -fr *.o<\/p>\n<p>\u9759\u6001\u94fe\u63a5\u5e93\u7684\u8c03\u7528<br \/>\nMakefile\u5982\u4e0b\uff1a<br \/>\n1\uff09\u7f16\u8bd1\u7684\u65f6\u5019\u9700\u8981\u901a\u8fc7INCLUDE\u6307\u660e\u5934\u6587\u4ef6\u7684\u8def\u5f84<br \/>\n2\uff09\u94fe\u63a5\u7684\u65f6\u5019\u9700\u8981\u901a\u8fc7LDFLAGS \u548c LIBS\u6307\u660e\u9759\u6001\u5e93\u7684\u8def\u5f84\u548c\u540d\u79f0\u3002\u8fd9\u91cc\u4e0d\u9700\u8981\u50cf\u52a8\u6001\u5e93\u90a3\u6837\u201c\u6390\u5934\u53bb\u5c3e\u201d\uff0c\u76f4\u63a5\u5199\u4f5cfunc.a\u5373\u53ef\u3002<br \/>\n3\uff09\u6267\u884c\u7684\u65f6\u5019\uff0c\u4e0d\u9700\u8981\u62f7\u8d1dfunc.a\u81f3\u73af\u5883\u53d8\u91cf\u5305\u542b\u7684\u8def\u5f84\uff0c\u76f4\u63a5\u6267\u884c\u5373\u53ef\u3002<\/p>\n<p> #######################<br \/>\n# Makefile<br \/>\n#######################<\/p>\n<p># target<br \/>\nTARGET := main<\/p>\n<p># compile and lib parameter<br \/>\nCC      := g++<br \/>\nLDFLAGS := -L.<br \/>\nLIBS    := func.a<br \/>\nDEFINES :=<br \/>\nINCLUDE := -I.<br \/>\nCFLAGS  :=<br \/>\nCXXFLAGS:= <\/p>\n<p># link<\/span>(TARGET):main.o<br \/>\n    <span class=\"katex math inline\">(CC) -o<\/span>@ <span class=\"katex math inline\">^<\/span>(LIBS)<br \/>\n#compile<br \/>\nmain.o:main.cpp<br \/>\n    <span class=\"katex math inline\">(CC) -c<\/span>^ -o <span class=\"katex math inline\">@<\/p>\n<p># clean<br \/>\nclean:<br \/>\n    rm -fr *.o<br \/>\n    rm -fr<\/span>(TARGET)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>main.cpp\u6e90\u6587\u4ef6\uff1a\u5305\u542b\u5165\u53e3\u51fd\u6570 int main()\uff0c\u8be5\u6e90\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e86\u201cfunc.h\u201d\u5934\u6587\u4ef6\uff0c\u5728\u5165\u53e3\u51fd\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-2551","post","type-post","status-publish","format-standard","hentry","category-embedded"],"_links":{"self":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/2551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2551"}],"version-history":[{"count":1,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/2551\/revisions"}],"predecessor-version":[{"id":2552,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/2551\/revisions\/2552"}],"wp:attachment":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2551"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}