Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cmake in Cracket +

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

I would like to share with you how to use cmake in CCompact +. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.

1. Compilation of a single source file in hello.c--

/ / hello.c#include int main () {puts ("hello, world!"); return 0;}

To compile and generate the corresponding executable, you might use the following command:

$cc-ohello hello.c$. / hellohello, world!

However, if you use make (as long as your operating system already has GCC and GNU Make installed), it will look a little more refreshing.

$make hellocc hello.c-ohello $. / hellohello, world!

1.1 write Makefile

What? You don't even bother to write "make hello"? After reading this section, your "delusions" should be realized, and then you just need to type out four letters-"make" slowly, and then press the enter key, which is more convenient than the graphical interface IDE (at least you don't have to look around for the damn "run" button.

At this point, all you have to do is create a new file Makefile in the same directory in hello.c as the configuration file for the make command. Its content is simple:

Hello:

1.2 set the compiler

What? Instead of using the default cc, you want to use gcc to compile the program? That's not easy, just assign the value of the CC variable to gcc in the Makefile file.

CC: = gcchello:

If you want to run make to test the effect at this time, please note that make will not be recompiled to generate hello at all. Why? Because make is "lazy", because it detects that hello.c is exactly the same as the last compilation, and then recompiles the resulting executable must also be the same, then there is no need to run, directly return the results. At this time, you can use some "small tricks". Anyway, make is very gullible. Enter the following command to update the last modified date of hello.c.

$touch hello.c

Or simply delete the hello file. But there are both high and low ways to delete files, if you use the following command:

$rm-f hello

Then this is a low move, because it is likely to delete other very important source files by mistake, resulting in very serious consequences. So what's the trick? That is to add the following to the Makefile:

Clean: $(RM) hello

The mode of operation is also very simple, just run the make clean command.

1.3 increase compilation options

If you want to add the-g-Wall-Wextra option to gcc, just set the value of the variable CFLAGS.

CC: = gccCFLAGS: =-g-Wall-Wextrahello:clean: $(RM) hello

At this point, the result of running make clean and make is as follows:

$make cleanrm-f hello$ makegcc-g-Wall-Wextra hello.c-o hello

two。 Block compilation-compiling programs with multiple source files

If the program no longer has only one source file, then combined with the built-in compilation rules of Make, you can also write Makefile files very succinctly to complete the compilation task. Here is a simple example:

LDLIBS: =-lncursesblock: block.o function.oblock.o function.o: function.hclean: $(RM) * .o $(RM) block

The program needs to use ncurses, which is a basic library for screen control under the character terminal, so the-lncurses option needs to be added at the end of the compilation. At this time, you may have found that, in fact, the main content of writing Makefile is to write dependencies. Block: block.o function.o means that the executable file block is generated by block.o and function.o links. At the same time, bolck.o and function.o are compiled and generated from bolck.c and function.c as needed, because make has the following built-in rules: * .o is generated by a c source file with the same name, so you don't have to write extra bolck.o:bolck.c and function.o:function.c. The running result is as follows

$makecc-c-o block.o block.ccc-c-o function.o function.ccc block.o function.o-lncurses-o block

Built-in rules for 3.Make

Enter the make-p command to view all the built-in rules of make. For example, the * .o mentioned above is generated by a c source file with the same name, which is shown in the output of make-p:

% .o:% .c # recipe to execute (built-in): $(COMPILE.c) $(OUTPUT_OPTION) $

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report