


How to compile and execute c program in Linux environment? (detailed explanation)
The content of this article is to introduce how to compile and execute c programs in the Linux environment? (detailed explanation). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1 Compilation and execution of a single file
Create the main.c file with the following content:
#include <stdio.h>#include <stdlib.h>int main(void){ printf("Hello world!\n");return 0; };</stdlib.h></stdio.h>
Compilation:
gcc -o main main.o
Execution:
root@ubuntu:/ybg/python# ./main Input an integer:10sum=55
2 Compilation and execution of multiple files
Create the sum.c file with the following content:
#include <stdio.h>#include <stdlib.h>int sum(int x){int i, result=0;for(i=0; i 100) exit(-1);return result; };</stdlib.h></stdio.h>
Create the main.c file with the following content :
#include <stdio.h>#include <stdlib.h>int main(void){int x; printf("Input an integer:\n"); scanf("%d", &x); printf("sum=%d\n", sum(x));return 0; };</stdlib.h></stdio.h>
Compile
gcc -c sum.c -fPIC -o sum.ogcc -c main.c -fPIC -o main.o
Generate an executable file, the file name is main
gcc -o main sum.o main.o
Execution program
./main
The execution result is the same as above
3 Use the dynamic link library method
Generate the dynamic link library
gcc sum.o -shared -o sum.so
Generate the executable file, the file name is main
gcc -o main sum.o main.o
Execute
./main
If the following error is reported, it means that the newly generated sum.so was not found in the default dynamic link library path
./main: error while loading shared libraries: sum.so: cannot open shared object file : No such file or directory
Execute the following command to add the current directory to the dynamic link library search path environment variable
export LD_LIBRARY_PATH=pwd:$LD_LIBRARY_PATH
Execute again
./main
The execution result is the same as above
4 python calls the .so dynamic link library
Create the test.py file with the following content:
import ctypes so = ctypes.CDLL('./sum.so')print "so.sum(50) = %d" % so.sum(50)
Execute
root@ubuntu:/ybg/python# python test.py so.sum(50) = 1275
The above is the detailed content of How to compile and execute c program in Linux environment? (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Indispensable commands in Linux include: 1.ls: list directory contents; 2.cd: change working directory; 3.mkdir: create a new directory; 4.rm: delete file or directory; 5.cp: copy file or directory; 6.mv: move or rename file or directory. These commands help users manage files and systems efficiently by interacting with the kernel.

In Linux, file and directory management uses ls, cd, mkdir, rm, cp, mv commands, and permission management uses chmod, chown, and chgrp commands. 1. File and directory management commands such as ls-l list detailed information, mkdir-p recursively create directories. 2. Permission management commands such as chmod755file set file permissions, chownuserfile changes file owner, and chgrpgroupfile changes file group. These commands are based on file system structure and user and group systems, and operate and control through system calls and metadata.

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
