-3

OS: Ubuntu 14.04 (Trusty Tahr)

Write two .c files named dtest1.c and dtest2.c.

File dtest1.c

int p = 2;

void print()
{
    printf("this is the first DLL test!\n");
}

File dtest2.c

int p = 3;

void print()
{
    printf("this is the second DLL test!\n");
}

Then, compile them to get two files named dtest1.so and dtest2.so:

gcc -O -fpic -shared -o dtest1.so dtest1.c
gcc -O -fpic -shared -o dtest2.so dtest2.c

Write a .c file named dtest3.c.

File dtest3.c

#include "dtest1.so"

int main ()
{
    print();
    return 0;
}

So far now, everything is good. There isn't any error (only a warning).

Then:

gcc -o dtest3 dtest3.c dtest1.so

Error:

In file included from dtest3.c:1:0:
dtest1.so:17:1: warning: null character(s) ignored [enabled by default]
dtest1.so:17:2: error: stray ‘\260’ in program
   ......
   ......    /*omit too many similar information */
dtest1.so:18:2: error: stray ‘\212’ in program
dtest1.so:18:2: error: stray ‘\1’ in program
In file included from dtest3.c:1:0:
dtest1.so:18:956: warning: null character(s) ignored [enabled by default]
In file included from dtest3.c:1:0:
dtest1.so:18:2: error: stray ‘\244’ in program
dtest1.so:18:2: error: stray ‘\1’ in program
In file included from dtest3.c:1:0:
dtest1.so:18:980: warning: null character(s) ignored [enabled by default]
dtest1.so:18:982: warning: null character(s) preserved in literal [enabled by default]
dtest1.so:18:982: warning: missing terminating " character [enabled by default]
In file included from dtest3.c:1:0:
dtest1.so:18:2: error: missing terminating " character

What's wrong with that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lie Zang
  • 5
  • 3
  • 5
    You don't `#include` compiled libraries, you `#include` C source code, normally placed in a file with `.h` extension. – n. m. could be an AI Aug 09 '15 at 18:56
  • thank you for your words ,you inspired me to solve this problem.^_^ – Lie Zang Aug 10 '15 at 09:47
  • This is a common error (including object/binary files). What is the canonical question? – Peter Mortensen Jun 04 '23 at 14:34
  • Inadvertently included object/binary files (thus not real duplicates): *[Daemon on embedded Linux device using BusyBox be written in C or as a script](https://stackoverflow.com/questions/28759855/)* and *[Cannot use 'sourceCpp' from a file](https://stackoverflow.com/questions/48547750/cannot-use-sourcecpp-from-a-file#comment134363065_48547750)* (the titles are not descriptive wrt. to this kind of error and thus the bodies must be read) – Peter Mortensen Jun 04 '23 at 14:46
  • The warning probably contains "`warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]`" – Peter Mortensen Jun 04 '23 at 15:17
  • The ***actual*** first error more likely contains the signature for ELF (0xFF, octal 177), "`dtest1.so:1:1: error: stray ‘\177’ in program`" (tested on [Ubuntu 18.04](https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_18.04_LTS_.28Bionic_Beaver.29) (Bionic Beaver)). This is in line with the symptom from the [former reference](https://stackoverflow.com/questions/48547750/cannot-use-sourcecpp-from-a-file#comment134363065_48547750) (though not the exact same cause); see also its [***"Linked"*** questions](https://stackoverflow.com/questions/linked/48547750?lq=1) (upper right). – Peter Mortensen Jun 04 '23 at 15:32

2 Answers2

3

Libraries are compiled code, not (textual) source code. #include simply inserts the contents of given file, so it should be source code. You have to link your library by passing it as argument to the linker.

Read here for more.

Youka
  • 2,646
  • 21
  • 33
-3

Writing a .h file like this:

dtest1.h

void print()
{
    printf("this is the first dll test!(this is a .h file)\n");
}

Modify the dtest3.c file, delete the #include "dtest1.so" and add a

#include "dtest1.h"

And compiled that:

gcc -o dtest3 dtest3.c dtest1.so

Some info to me:

In file included from dtest3.c:1:0:
dtest1.h: In function ‘print’:
dtest1.h:3:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
     printf("this is the fist dll test!(this is a .h file)\n");
     ^

Try to run dtest3:

./dtest3

Get info like this:

this is the fist dll test!(this is a .h file)

But I really confused it, so try again:

$ vi dtest1.h

Modify the file like this:

dtest1.h

void print();

And modify dtest1.c:

$ vi dtest1.c

Add a line:

#include "dtest1.h"

And compiled it to get a new .so file:

gcc -O -fpic -shared -o dtest1.so dtest1.c

Compiled the dtest3.c file:

gcc -o dtest3 dtest3.c dtest1.so

NO WARNING NO ERROR.....HAPPY

$ ./dtest3
./dtest3: error while loading shared libraries: dtest1.so: cannot open shared object file: No such file or directory

New problem....T_T ,but the result of search by google tell me :

system can't find the library, even though i provide the path of dtest1.so when i complite the dtest3.c ,but this information didn't writed into the dtest3

Do some test:

$ ldd dtest3

/* This command will tell the file's depended library */

linux-vdso.so.1 =>  (0x00007fff1d3d7000)
dtest1.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6e9914c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6e99532000)

/* Sadness flood my mind...... */

But a webpage tell me a method to solve it:

$export LD_LIBRARY_PATH=.

The system will find that path at first time, so can insured the dtest1.so could be found.

Now try again:

$ ./dtest3

this is the first dll test!

Congratulations!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lie Zang
  • 5
  • 3
  • Instead of `$export LD_LIBRARY_PATH=.` you could simply update the link directories cache of your system by program `ldconfig`. – Youka Aug 11 '15 at 01:07