In Eclipse, C + + compiles. So library, using the. So Library (Linux)

Source: Internet
Author: User
Tags lunix

first, you need to install the Eclipse's CDT compilation environment. Configure the GCC compilation environment. LUnix usually have GCC, Windows needs to install a Cygwin.

first, establish a dynamic database (. so file)

1. Create a shared library project in eclipse first

File->new->project->c project, select Shared Library, select Empty Project

2, create the project source file Share.cpp

File->new->source Folder, specifying the name SRC

File->new->source file, specifying a name of share

The last directory structure:



3. Write C code and C header file


#include <stdio.h>
#include <stdlib.h>

void SayHello () {

	printf ("Hello!!! 1 ");

}

Header file:


void SayHello ();

3. Build Project

The error message will appear as follows:

Build of configuration Debug for project share * * *


Make all
Building file:. /src/share.c
INVOKING:GCC C Compiler
Gcc-o0-g3-wall-c-fmessage-length=0-mmd-mp-mf "SRC/SHARE.D"-MT "SRC/SHARE.D"-O "src/share.o" ". /src/share.c "
Finished building:. /src/share.c

Building target:libshare.so
INVOKING:GCC C Linker
Gcc-shared-o "libshare.so"./SRC/SHARE.O
/USR/BIN/LD:./src/share.o:relocation r_x86_64_32 against '. Rodata ' can is used when making a shared object; Recompile with-fpic
./src/share.o:could not read Symbols:bad value
COLLECT2:LD return 1
Make: * * * * [libshare.so] Error 1


Build Finished * * * *

The error message indicates that a-fpic parameter is required at compile time.

This is the missing-fpic parameter.

Gcc-o0-g3-wall-c-fmessage-length=0-mmd-mp-mf "SRC/SHARE.D"-MT "SRC/SHARE.D"-O "src/share.o" ". /src/share.c "

Need to add-fpic:

project->properties->c/c++ build-> setting->gcc C compiler is modified from the original GCC to commend

Compile the pass again. The information is as follows:

Build of configuration Debug for project share * * *


Make all
Building file:. /src/share.c
INVOKING:GCC C Compiler
Gcc-fpic-o0-g3-wall-c-fmessage-length=0-mmd-mp-mf "SRC/SHARE.D"-MT "SRC/SHARE.D"-O "src/share.o" ". /src/share.c "
Finished building:. /src/share.c

Building target:libshare.so
INVOKING:GCC C Linker
Gcc-shared-o "libshare.so"./SRC/SHARE.O
Finished building target:libshare.so



Build Finished * * * *


Second, call the compiled. so file.

1. Create an executable project in eclipse first

File->new->project->c project, select Execuable Library, select Empty Project

2, create the project source file Share.cpp

File->new->source Folder, specifying the name SRC

File->new->source file, specifying a name of HELLO.C

3, copy the share header file, because you need to reference the Shar.so function. Writing hello.c files


Here you will compile with reference to the Share.so shared library that you just created:

Libraries citation path and library name in project->properties->c/c++ build-> SETTING->GCC c Linker



Otherwise, there will be no function problems found as follows:

Build ' configuration Debug for Project Hello * * * *


Make all
Building Target:hello
INVOKING:GCC C Linker
Gcc-o "Hello"./src/hello.o
./src/hello.o:in function ' main ':
/ws/jack/android/projects/testporject/hello/debug/.. /src/hello.c:11:undefined reference to ' SayHello '
COLLECT2:LD return 1
Make: * * * [Hello] Error 1


Build Finished * * * *


Third, the operation of the project

Error...

/ws/jack/android/projects/testporject/hello/debug/hello:error while loading shared libraries:libshare.so:cannot Open Shared object File:no such file or directory

The library file could not be found.


Originally LUnix to run C + + program ran to the default path to find this share library file, but certainly can't find. Because the library is not placed in the default path.

There are two ways to specify the path to the Run-time library file:

One is to set the system's LD_LIBRARY_PATH environment variable (invalid program in Eclipse to set up an external official run).

The other is to compile the path of the dynamic link library file into the binary executable file. -r+ path.


I use Ld_library_path to run the environment variable to set the path. Set the path to libshare.so.

Project->run as->run configurations->environment




Reference:

Http://blog.sina.com.cn/s/blog_7769660f01011pf1.html

http://hi.baidu.com/sfzhaoif/item/32ecf6bfe055b542bb0e1209

Http://stackoverflow.com/questions/14448343/cannot-open-shared-object-file-no-such-file-or-directory-running-or-debugging

Http://www.linuxidc.com/Linux/2012-08/68828.htm

Run again, you can come out and call the result of the





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.