Solaris operating system doing C application development steps _c language

Source: Internet
Author: User

1, the development of C program necessary tools and settings

A,development tools under Solaris

Developing the C application under Solaris, the default development environment is Sun Studio. Currently, Sun Studio 11 is the Sun's newest C/c++/fortran development tool, which has been installed on the UNIX Experience Center server, which we can use directly. In Sun Studio, the C program's compiler tool name is CC. There is also the make this command, the Make command is Solaris installed since the bring, do not require us to install additional.

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, and in general, you can use the Export command for this setting. Suppose that the shell you are using is Bourne Shell,sun Studio 11 installation directory for/opt/sunstudio_11/sunwspro,make is usually installed in the/usr/ccs/bin directory, you can use the following command to set:

Copy Code code as follows:

# Path=/opt/sunstudio_11/sunwspro/bin:/usr/ccs/bin: $PATH
# Export PATH

(To be sure, this environment variable has been set up early in the UNIX Experience Center server and does not require the user to set it up.) )

2 . Develop a simple program with only one file

If your C application contains only one source file, you do not need to compile with makefile, as an example of a simple Hello World program, for example, we have a simple C program hello.c, whose contents are shown below:

Copy Code code as follows:

#include <stdio.h>

Int
Main ()
{
printf ("Hello world\n");
}

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

Copy Code code as follows:

# Cc-o Hello hello.c

Or:

Copy Code code as follows:

# Make Hello

Both of these methods directly generate the application Hello, the former method is to generate the target code directly with the Compile command cc. In the latter case, if you use make to generate the target code, this method only works for compiling a source file, and note that the parameter behind make should be the filename of the C source program. The front part, that is, hello.

3 . Develop c application with multiple files

If we have multiple source files, for example, there is a main program, another specific function implementation, plus a header file, here or with Hello World as an example to illustrate that the example has three source program files, two for C source files, the main program hello.c and concrete function implementation Hello_ F.C, one for C header files, respectively:

The following are the contents of source code hello.h:

Copy Code code as follows:

void Hello ();

The following are the contents of source code hello.c:

Copy Code code as follows:

#include "hello.h"

Int
Main ()
{
Hello ();
}

The following are the contents of source code HELLO_F.C:

Copy Code code as follows:

#include

void
Hello ()
{
printf ("Hello world\n");
}

We can use a command to complete this compilation task:

Copy Code code as follows:

# Cc-o Hello hello.c hello_f.c

But in the system, we'll find that this command produces three files, hello,hello.o and HELLO_F.O, so how are the three files generated and what is the whole compilation process? For the sake of simplicity, we use the following three commands to illustrate the process of producing these files, presumably by:

Copy Code code as follows:

# 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 example above, we use the command line to compile the program directly, however, in the actual work, a project only one or two source code files are very few, large projects will often have thousands of source code files, if we just use simple command line to compile these files, not only each time to write a bunch of commands, error-prone, And it is difficult to maintain the consistency of the compilation options, in this case, we will use makefile to do the corresponding compilation organization and management. Or take the Hello world above as an example to illustrate how to write a simple makefile to compile the program.

We write a simple makefile to compile this simple project HelloWorld.

The following are the contents of our makefile:

Copy Code code as follows:

All:hello

HELLO:HELLO.O HELLO_F.O
Cc-o Hello 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, the actual project, we encounter more complex makefile, about makefile grammar and rules, can get a lot of helpful tutorials to learn, here is not to focus on the introduction.

Once the makefile is generated, we can use it to compile the program, as follows

Copy Code code as follows:

# make
Cc-c hello.c
Cc-c HELLO_F.C
Cc-o Hello hello.o hello_f.o

Thus, the target code is generated smoothly.

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.