C + + compiled so with dynamic load so

Source: Internet
Author: User

C File FILEA.C:

#include <stdio.h>void fun1 () {printf ("I am from Filea fun1 \ n");}

C File FILEB.C:

#include <stdio.h>void fun2 () {printf ("I am from Fileb fun2 \ n");}

Test file testso.c

void Fun1 (), void fun2 (), int main (int argc,char *argv[]) {fun1 (); fun2 (); return 0;}


1. Compile the filea.c fileb.c into the so library for TESTSO.C invocation, as follows:


gcc-wall-fpic-shared filea.c Fileb.c-o first.so




2. Compile the testso.c and link the shared library first.so

Gcc-wall testso.c First.so-o Testso




3. Implementation.

At this point, execution./testso will be thrown ./testso:error while loading shared libraries:first.so.0:cannot open shared object file:no such F Ile or directory

The program Testso has been completed, but to run it must be able to locate the shared library first.so, because the functions in the library are loaded when the program is run. It is important to note that the current working directory may not be in the lookup path of the shared library, so you need to set the environment variable ld_library_path using the following command line:


$ export ld_library_path= $LD _library_path:./


Execute the./testso again and output the correct result:






C + + compiled so with dynamic load so

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.