guides:software:make:start
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
guides:software:make:start [2020/07/20 15:19] – wikiadmin | guides:software:make:start [2024/07/25 15:01] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 3: | Line 3: | ||
Make is a build automation tool that is designed to automatically construct executable programs from source code. | Make is a build automation tool that is designed to automatically construct executable programs from source code. | ||
+ | |||
+ | Make simplifies the build process for projects at all levels. | ||
===== Simple Use ===== | ===== Simple Use ===== | ||
Line 261: | Line 263: | ||
</ | </ | ||
- | Secondly, the default target has been changed. | + | Secondly, the default target has been changed. |
<code make> | <code make> | ||
Line 274: | Line 276: | ||
</ | </ | ||
+ | The final makefile for this section is: | ||
+ | <code make> | ||
+ | CXXFLAGS = -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion | ||
+ | |||
+ | OBJS = hello greeting salute | ||
+ | |||
+ | all: ${OBJS} | ||
+ | |||
+ | clean: | ||
+ | rm -f ${OBJS} | ||
+ | </ | ||
===== Makefiles for Multiple File Compilation ===== | ===== Makefiles for Multiple File Compilation ===== | ||
+ | |||
+ | One of the most powerful uses of make is to compile multiple files into a single executable. | ||
+ | |||
+ | ==== Example ==== | ||
+ | |||
+ | For this example the code is decomposed into the following files: | ||
+ | |||
+ | * // | ||
+ | * <code c++> | ||
+ | #ifndef STRING_TOOLS | ||
+ | #define STRING_TOOLS | ||
+ | |||
+ | void LowerCase(std:: | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | * // | ||
+ | * <code c++> | ||
+ | include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | void LowerCase(string & word){ | ||
+ | for(auto &x : word) { | ||
+ | x = static_cast< | ||
+ | } | ||
+ | |||
+ | return; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | * // | ||
+ | * <code c++> | ||
+ | #ifndef GREETING | ||
+ | #define GREETING | ||
+ | |||
+ | std::string GreetingString(std:: | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | * // | ||
+ | * <code c++> | ||
+ | |||
+ | #include < | ||
+ | |||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | string GreetingString(string language){ | ||
+ | string greeting = "Hello World"; | ||
+ | |||
+ | LowerCase(language); | ||
+ | |||
+ | if (language == " | ||
+ | | ||
+ | } else if (language == " | ||
+ | | ||
+ | } | ||
+ | |||
+ | return greeting; | ||
+ | } | ||
+ | </ | ||
+ | * // | ||
+ | * <code c++> | ||
+ | #include < | ||
+ | |||
+ | #include " | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | string languages[] = {" | ||
+ | |||
+ | for (auto lang: languages) { | ||
+ | cout << " The greeting in \"" | ||
+ | cout << GreetingString(lang) << " | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | * // | ||
+ | * <code make> | ||
+ | CXXFLAGS = -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wu | ||
+ | ninitialized -Wshadow -Wconversion -std=c++14 | ||
+ | |||
+ | OBJS = hello | ||
+ | |||
+ | all: ${OBJS} | ||
+ | |||
+ | hello: greeting.o stringTools.o | ||
+ | |||
+ | greeting.o: greeting.h stringTools.h | ||
+ | |||
+ | stringTools.o: | ||
+ | |||
+ | clean: | ||
+ | rm -f ${OBJS} *.o | ||
+ | </ | ||
+ | |||
+ | To build this code, you are required to compile stringTools.cpp into stringTools.o, | ||
+ | |||
+ | This could be accomplished by hand by executing the following | ||
+ | < | ||
+ | g++ -c greeting.cpp | ||
+ | g++ -c stringTools.cpp | ||
+ | g++ -o hello hello.cpp stringTools.o greeting.o | ||
+ | </ | ||
+ | |||
+ | Of course, if you have command line flags, this becomes much more complex. | ||
+ | |||
+ | Using the above // | ||
+ | |||
+ | < | ||
+ | $ make | ||
+ | g++ -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion -std=c++14 | ||
+ | g++ -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion -std=c++14 | ||
+ | g++ -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion -std=c++14 | ||
+ | $ ls | ||
+ | Makefile | ||
+ | greeting.cpp | ||
+ | </ | ||
+ | |||
+ | Note that all components are compiled and the final executable is produced. | ||
+ | |||
+ | < | ||
+ | $ touch greeting.cpp | ||
+ | $ make | ||
+ | g++ -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion -std=c++14 | ||
+ | g++ -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion -std=c++14 | ||
+ | </ | ||
+ | |||
+ | ==== The Makefile ==== | ||
+ | |||
+ | In order to build this project using make, you need to inform make of the dependencies built into the project. | ||
+ | |||
+ | <code make> | ||
+ | hello: | ||
+ | </ | ||
+ | |||
+ | You do not need to tell make that //hello// depends on // | ||
+ | |||
+ | You also do not need to tell make that //hello// depends on // | ||
+ | |||
+ | <code make> | ||
+ | stringTools.o: | ||
+ | </ | ||
+ | |||
+ | This line tells make that // | ||
+ | |||
+ | <code make> | ||
+ | greeting.o: greeting.h stringTools.h | ||
+ | </ | ||
+ | |||
+ | This line tells make that // | ||
guides/software/make/start.1595258385.txt.gz · Last modified: 2024/07/25 15:01 (external edit)