"Go" Multiple Files directory makefile file recursively execute compile all C files

Source: Internet
Author: User

First of all, the purpose of this nested execution of the makefile file: Just make the root directory of the makefile file, you can compile all C files, including sub-directories.

Meaning: The automated compilation behavior, in the future compile their own C files can be directly copied these makefile files to the appropriate directory can be easily compiled all files. These makefile files are generic and only need to change a little bit according to your engineering situation. The following will say.

The overall idea is: put the target file in the Debug folder under the Obj directory, the final binary file in the Debug folder under the Bin directory, how to recursively compile all except the Debug directory Makefile file: Get all subdirectories under the current directory, Executes the makefile file under the subdirectory, obtains all C files under the current directory, compiles the C file and places it under the specified destination folder. Finally, execute the makefile file in the Debug directory to generate the bin file.

Note: In addition to the Debug folder is special, the other subdirectories need to have makefile files, and these makefile are the same, except for the root directory makefile files are somewhat different. The makefile file is required for directories other than the bin and obj directories, even if there are no C files or other directories under the directory.

Process: First create a new debug folder under the root directory, the Debug folder has the Bin directory and the obj directory and a makefile file, such as structure. (The makefile file in this debug file needs to be last executed) (the tree tool needs to be installed on its own, under Ubuntu Direct input sudo apt-get install tree, but sometimes you may need to first sudo apt-get update)

The entire directory results as:

Then in the root directory to create a new makefile file, the root directory may also have C files, it is also necessary to handle the root directory C file, the contents are as follows:

#Set CompilerCc=GCC#the makefile file in the Debug folder needs to be executed last, so the subdirectory to be executed here is to exclude the Debug folder, which uses awk to exclude the Debug folder and read the remaining folderssubdirs=$ (Shell Ls-l |grep^d | Awk'{if ($$9! = "Debug") Print $$9}')#There is no need for the next line of comment code, because we already know that debug makefile is the last execution, so finally go directly to the debug directory to execute the specified makefile file on the line, the following comments #debug=$ (Shell ls-l | grep ^ D | awk ' {if ($$9 = = "Debug") Print $$9} ') #记住当前工程的根目录路径Root_dir=$ (shell pwd)#the name of the final bin file can be changed to the one you needbin=MyApp#directory where the destination file residesobjs_dir=debug/obj#the directory where the bin file residesbin_dir=debug/Li#Gets the C file set under the current directory, placed in the variable Cur_sourceCur_source=${wildcard *.C}#Place the corresponding C file name in the CUR_OBJS variable after converting it to O .Cur_objs=${patsubst%.c,%.O,$ (cur_source)}#Export the following variables into a child shell, which is equivalent to exporting to a makefile in a sub-directoryexport CC BIN objs_dir bin_dir root_dir#Note The order here, you need to execute subdirs last to be debugAll$ (subdirs) $ (CUR_OBJS) DEBUG#recursive execution of makefile files under subdirectories, which is the key to recursive execution$ (Subdirs):ECHO Make-C [Email protected]debug:ECHO#go directly to the debug directory to execute the makefile fileMake-C Debugecho:@echo$ (subdirs)#compile the C file into an O file, and place it in the directory where the target file is specified Objs_dir$ (CUR_OBJS):%.o:%.C $ (CC)-C $^-O $ (root_dir)/$ (objs_dir)/[Email Protected]clean:@rm$ (objs_dir)/*.o@rm-RF $ (bin_dir)/*

The above note is very detailed, the specific command if not clear, you can Google, for example: wildcard Patsubst awk, etc.

Readers can change their own debug directory and target file directory and bin file directory according to their needs.

The contents of the makefile file under other subdirectories are as follows:

#the subdirectory's makefile directly reads its subdirectories .subdirs=$ (Shell Ls-l |grep^d | Awk'{print $$9}')#The following is an interpretation of the same code of makefile under the same root directoryCur_source=${wildcard *.C} Cur_objs=${patsubst%.c,%.O,$ (cur_source)}all:$ ( subdirs) $ (CUR_OBJS) $ (subdirs):ECHO Make-C [email protected]$ (CUR_OBJS):%.o:%.C $ (CC)-C $^-O $ (root_dir)/$ (objs_dir)/[Email Protected]echo:@echo$ (Subdirs)

The makefile file under the Debug directory is as follows:

objs=*.oodir=obj$ (Root_dir)/$ (bin_dir)/$ (BIN): $ (ODIR)/$ (OBJS) $ (CC)-o [email protected] $^

Finally just under the root directory, my is my root directory makefile directory, execute make command can:

The result directory results are:

Then execute ". Debug/bin/myapp "can be done; Finally, make clean clear all the target files and bin files.

References are: http://blog.csdn.net/zplove003/article/details/7066595

For the preparation of the makefile file, see the Link: Write Makefile and http://wiki.ubuntu.org.cn/index.php?title=%E8%B7%9F%E6%88%91%E4%B8%80%E8% with me B5%b7%e5%86%99makefile&variant=zh-cn

"Go" Multiple Files directory makefile file recursively execute compile all C files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.