Preface Under Linux, compilation and linking require the use of Makefile, but writing a good Makefile is not easy, and writing a standard Makefile is even more troublesome. We can use autoconf to generate a Makefile, which can save a lot of trouble, especially in large projects. Here I will use a project to illustrate how to do it. Some steps are not required, but if you can't tell which steps are not required, it's best to follow them step by step, because some steps are omitted and the code also needs to be modified accordingly (such as modifying the path, etc.). step First install autoconf Create a new project folder (for example: joy) Create two new folders lib and src in it to store the library files and source files of the project. As shown in the following figure: Enter the lib folder, create a new header file, such as haha.h, and declare a function at random. //haha.h #include <stdio.h> void printhaha(); Enter the src folder, create a new C file, such as haha.c, to implement the functions of the header file; create a new C file, such as main_code.c, to write the main function. //haha.c #include "../lib/haha.h" void printhaha() { printf("haha\n"); } //main_code.c #include "../lib/haha.h" void main() { printhaha(); } Back to the joy folder, our project files have been written Run autoscan to generate configure.scan Edit configure.scan The file before editing is like this #Edit the original file before# -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) AC_CONFIG_SRCDIR([src/haha.c]) AC_CONFIG_HEADERS([config.h]) # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_OUTPUT
#Modified Files# -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) AC_INIT(hhh, 1.0, [email protected]) AC_CONFIG_SRCDIR([src/haha.c]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_OUTPUT(Makefile) Save and rename configure.scan to configure.ac Create a new Makefile.am Enter the following code AUTOMAKE_OPTIONS= \ foreign \ subdir-objects bin_PROGRAMS=test_prj test_prj_SOURCES= \ lib/haha.h \ src/haha.c \ src/main_code.c
Generate aclocal.m4 file using aclocal Generate configure file with autoconf Generate config.h.in with autoheader To generate Makefile.in with automake, you need to add --add-missing to automatically add the default file Then our software is ready, and we can upload the entire package to github or an open source forum. How do others compile and link executable files after downloading our source code package? Or how do we compile and link our executable files? The following are the general steps for source code installation, which are also the steps for our compilation and linking First generate the Makefile Then make Now we can see that the executable file test_prj has been generated Run it and the results are as follows: The result is correct. Entering the src directory, we found two more files This is the role of subdir-objects in AUTOMAKE_OPTIONS. Without this sentence, the two .o files will appear in your root directory. If unfortunately your project is large and there are files with the same name, there may be serious consequences, so it is recommended to add this sentence. Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links You may also be interested in:
|
<<: Detailed explanation of mysql record time-consuming sql example
>>: MySql inserts data successfully but reports [Err] 1055 error solution
SSH public key authentication is one of the SSH a...
Today, when I was using VMware to install a new v...
It is standard for websites to enable SSL nowaday...
1. Demand The local test domain name is the same ...
Creation of a two-dimensional array in Js: First ...
Table of contents Require Implementation Code dat...
This article introduces a tutorial about how to u...
Table of contents 1. Installation 2. Use Echarts ...
Written in front I don’t know who first discovere...
When optimizing a website, we must learn to use e...
Table of contents HTTP hijacking, DNS hijacking a...
1. Implement a simple triangle Using the border i...
environment Linux 3.10.0-693.el7.x86_64 Docker ve...
For Centos installation of MySQL, please refer to...
title XML/HTML CodeCopy content to clipboard <...