Concise Tutorial: How to Develop C applications on the Solaris operating system

Source: Internet
Author: User

 

1. Develop CProgramNecessary tools and settings

Development Tools under a and Solaris

Develop C applications under Solaris. The default development environment is Sun Studio. Currently, Sun Studio 11 is Sun's latest C/C ++/Fortran development tool. It has been installed on a server in the Unix experience center and can be directly used. In Sun Studio, the C program compilation tool is named CC. There is also the make command, which comes with the make command after Solaris is installed. We do not need to install it separately.

B. Set Environment Variables

To develop a C application under Solaris, you need to add the path of the compiler and make to the environment variable path. Generally, you can use the Export command to set the path. Assume that the shell you use is the Bourne shell, and the installation directory of Sun Studio 11 is/opt/sunstudio_11/sunwspro. Make is usually installed in the/usr/CCS/bin directory, you can use the following command to set the parameters:

# Path =/opt/sunstudio_11/sunwspro/bin:/usr/CCS/bin: $ path
# Export path

(It should be noted that on the server of the UNIX experience center, this environment variable has been set in advance and does not need to be set by the user .)

2. Develop simple programs with only one file

If your C application contains only one source program file, you do not need to use makefile during compilation. The following uses a simple hello World Program as an example, for example, we have a simple C program Hello. c. For details, see:

# Include <stdio. h>

Int main ()
{
Printf ("Hello World/N ");
}

When compiling this program, you can use the following two methods,

#CC-O hello. c

Or:

# Make hello

Both methods can directly generate the application hello. The previous method is to directly generate the target using the compilation command CC.Code. The next method is to use make to generate the target code. This method is only applicable to compiling a source file. Note that the parameter after make should be the. part before the file name of the C source program, that is, hello.

3. Develop a C application containing multiple files

If we have multiple source files, for example, one is the main program, the other is a specific function implementation, and the other is a header file, we will use Hello world as an example to illustrate it, in this example, there are three source program files, two of which are C source files and the main program Hello. C and the specific function implementation hello_f.c, one is the c header file, respectively:

As follows:Source codeHello. H content:

Void Hello ();

The following is the content of source code hello. C:

# Include "Hello. h"

Int main ()
{
Hello ();
}

The following is the content of the source code hello_f.c:

# Include <stdio. h>

Void Hello ()
{
Printf ("Hello World/N ");
}

We can use a command to complete the compilation task:

# Cc-O hello. c hello_f.c

But in the system, we will find that this command will generate three files, hello, hello. O and hello_f.o, how are these three files generated and the entire compilation process? To simplify the process, we use the following three commands to illustrate the process of generating these files:

# Cc-C hello. c
# Cc-C hello_f.c
# LD-O hello-LC hello. O hello_f.o

4. Use makefile to develop a project

In the above example, we use the command line to directly compile the program, but in actual work, a project has only one or two source code files, large projects often have thousands of source code files. If we compile these files with simple command lines, not only do we need to write a bunch of commands each time, but it is prone to errors, in addition, it is difficult to keep the compilation options consistent. In this case, we will use makefile for the corresponding compilation organization and management. Let's take the preceding Hello world as an example to illustrate how to write a simple makefile to compile the program.

Let's write a simple makefile to compile this simple project helloworld.

The following is the content of our makefile:

ALL: Hello

Hello: Hello. O hello_f.o
CC-O hello. O hello_f.o
Hello. O: Hello. c
CC-C hello. c
Hello_f.o: hello_f.c
CC-C hello_f.c
Clean:
Rm-f *. O hello

This is just a very simple makefile. In actual projects, we encounter more complicated makefile. We can get a lot of useful tutorials on the makefile syntax and rules, I will not focus on it here.

After makefile is generated, we can use it to compile the program, as shown below:

# Make
CC-C hello. c
CC-C hello_f.c
CC-O hello. O hello_f.o

The target code is generated smoothly.

 

Author: Sun China Engineering Research Institute Zhang Wenlong

 

Reprinted statement:This article from http://www.unix-center.net /? P = 16

 

 

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.