Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compile gcc dynamically

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to compile gcc dynamically, in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Advantages: small size, fast compilation

Disadvantages: high dependence

The code is as follows:

[root@74-82-173217 shared] # cat add.c

Int add (int x, int y) {

Return x + y

}

Parsed in 0.007 seconds at 12.13 KB/s

Add.c summation function

The code is as follows:

[root@74-82-173217 shared] # cat print.c

# include

Void print (int x) {

Printf ("% d\ n", x)

}

Parsed in 0.007 seconds at 14.78 KB/s

Print print function

The code is as follows:

[root@74-82-173217 shared] # cat head.h

# ifndf HEAD_H

# define HEAD_H

Int add (int, int)

Void print (int)

# endif

Parsed in 0.007 seconds at 16.34 KB/s

Head.h declaration header file

The code is as follows:

[root@74-82-173217 shared] # cat main.c

# include

# include "head.h"

Int main () {

Int x = add (3,5)

Print (x)

}

Parsed in 0.007 seconds at 19.70 KB/s

Main.c principal function

1. Compile the dynamic library

[root@74-82-173217 shared] # gcc-fpic-shared add.c print.c-o libd.so

Generate a libd.so dynamic library using the-fpic-shared parameter

2. Generate the execution file

[root@74-82-173217 shared] # gcc main.c libd.so-o main

Load the dynamic library and generate the main execution file

3. Dynamic library loading

[root@74-82-173217 shared] #. / main

. / main: error while loading shared libraries: libd.so: cannot open shared object file: No such file or directory

Because we are compiling as a dynamic library, when executing main, the execution fails because the libd.so library cannot be found. You can use ldd main to check that libd.so is not found.

The code is as follows:

[root@74-82-173217 shared] # ldd main

Linux-gate.so.1 = > (0x0070c000)

Libd.so = > not found

Libc.so.6 = > / lib/i686/nosegneg/libc.so.6 (0x0050e000)

/ lib/ld-linux.so.2 (0x00ea6000)

Parsed in 0.000 seconds at 434.42 KB/s

There are three ways to solve this problem

1. Add the dynamic library path to the environment variable

[root@74-82-173217 shared] # export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/c/shared/

2. Add the dynamic library path to ld.so.conf

[root@74-82-173217 shared] # echo "/ root/c/shared" > > / etc/ld.so.conf

[root@74-82-173217 shared] # ldconfig

Re-search all dynamic libraries and update to / etc/ld.so.cache

[root@74-82-173217 shared] # ldconfig-v | grep libd.so

Find out whether the libd.so library is currently included

3. Copy directly to the system directory

[root@74-82-173217 shared] # cp libd.so / lib/

Gcc parameter

-shared:

This option specifies that a dynamic link library is generated (let the connector generate T-type export symbol tables and sometimes weak-link W-type export symbols), without which external programs cannot connect. Equivalent to an executable file

-fpic:

It means that it is compiled into location-independent code, and without this option, the compiled code is location-dependent, so dynamic loading is through code copy to meet the needs of different processes, but can not achieve the purpose of real code segment sharing.

This is the answer to the question about how to dynamically compile gcc. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report