Linux gcc compiles and writes a small program to verify its correctness.

Source: Internet
Author: User

It is particularly worth mentioning that there are many points worth learning about Linux gcc compilation. Here we mainly introduce Linux gcc compilation, including Linux gcc compilation. How can I use php to call a function compiled into the so file? One method is to write a php module (php extension), call the functions in this module in php, and then call the functions in so through this module. The following is a simple example. The operating system is Fedora Core 6.

First, create a simple so file: 1/*** 2 * hello. c 3 * To compile, use following commands: 4 * gcc-O-c-fPIC-o hello. o hello. c 5 * gcc-shared-o libhello. so hello. o 6 */7 8 int hello_add (int a, int B) 9 {10 return a + B; 11}

Then compile it into Linux gcc. so file and put it in the system: 1 $ gcc-O-c-fPIC-o hello. o hello. c2 $ gcc-shared-o libhello. so hello. o3 $ su4 # echo/usr/local/lib>/etc/ld. so. conf. d/local. conf5 # cp libhello. so/usr/local/lib6 #/sbin/ldconfig

Write a small program to verify its correctness: 1/*** // *** 2 * hellotest. c 3 * To compile, use following commands: 4 * gcc-o hellotest-lhello hellotest. c 5 */6 # include <stdio. h> 7 int main () 8 {9 int a = 3, B = 4; 10 printf ("% d + % d = % d \ n", a, B, hello_add (a, B); 11 return 0; 12}

Linux gcc compilation and execution: $ gcc-o hellotest-lhello hellotest. c $./hellotest3 + 4 = 7OK. Let's create the PHP module. First, make sure that you have installed the php-devel package. If not, find the package on the installation CD. Then download the php source code. I use php-5.2.3.tar.gz for decompression.

$ Tar xzvf php-5.2.3.tar.gz $ cd php-5.2.3/ext then build a module named hello using the following command. $./Ext_skel -- extname = hello after executing this command, it will prompt you which command should be used to compile the Linux gcc module. Unfortunately, it is the Linux gcc compilation method that integrates the module into php. If you want to compile Linux gcc into a Dynamically Loaded php_hello.so, the method is simpler.

$ Cd hello, first edit the config. m4 file, and remove the comments of line 16th and line 18th as dnl .) 1 16: PHP_ARG_ENABLE (hello, whether to enable hello support, 2 17: dnl Make sure that the comment is aligned: 3 18: [-- enable-hello Enable hello support]) then run the phpize program to generate the configure script:

$ Phpize then open php_hello.h and add the function declaration below PHP_FUNCTION (confirm_hello_compiled): 1PHP_FUNCTION (confirm_hello_compiled);/** // * For testing, remove later. */2 PHP_FUNCTION (hello_add); open hello. c. Add the following content under PHP_FE (confirm_hello_compiled, NULL.

1 zend_function_entry hello_functions [] = {2 PHP_FE (confirm_hello_compiled, NULL)/** // * For testing, remove later. */3 PHP_FE (hello_add, NULL)/** // * For testing, remove later. */4 {NULL, NULL, NULL}/** // * Must be the last line in hello_functions [] */5}; then in hello. at the end of c, write the content of the hello_add function:

1PHP_FUNCTION (hello_add) 2 {3 long int a, B; 4 long int result; 5 6 if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "ll", & a, & B) = FAILURE) {7 return; 8} 9 10 result = hello_add (a, B); 11 12 RETURN_LONG (result); 13} Save and exit. Compile and install Linux gcc: $. /configure $ make LDFLAGS =-lhello $ su # cp modules/hello. so/usr/lib/php/modules

Create a hello. php file under/var/www/html with the following content:

 
 
  1. <?php 
  2. dl("hello.so");  
  3. echo hello_add(3, 4);  
  4. ?> 

Then open the "hello. php" file in the browser. If "7" is displayed, the function is successfully called.

Linux knowledge supplement: Because Editplus is used, the default C language source program is saved. cpp, And then I compile it with Linux gcc in cygwin. Some undeclared functions are always displayed, but these functions are called by some standard systems. Why?

This problem was depressing for one afternoon and one night. Later, I did not know why I tried to change the source program. cpp to. c. As a result, the Linux gcc compilation was successful. Later, I learned on the Internet that in Linux, the Linux gcc compilation is used to compile the. c file, while the c ++ file uses the g ++ command to compile the c ++ source program.

  1. Describes the Linux GCC stability requirements.
  2. Understanding of Linux GCC 4.4
  3. Linux GCC static Link
  4. Linux gcc version upgraded
  5. Linux gcc concepts and Parameters

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.