cfyzium / bearlibterminal

Interface library for applications with text-based console-like output
Other
119 stars 18 forks source link

How to compile the simple example (in C) #1

Open edbird opened 4 years ago

edbird commented 4 years ago

Simple example: http://foo.wyrd.name/en:bearlibterminal#documentation

I am currently attempting to compile this with

g++ -I. -L./libBearLibTerminal.so main2.cpp -o app2.exe

however I obtain some errors such as

/usr/bin/ld: /tmp/ccKM74Fy.o: in function `main':
main2.cpp:(.text+0x5): undefined reference to `terminal_open'
/usr/bin/ld: main2.cpp:(.text+0x20): undefined reference to `terminal_refresh'
/usr/bin/ld: main2.cpp:(.text+0x25): undefined reference to `terminal_read'
/usr/bin/ld: main2.cpp:(.text+0x38): undefined reference to `terminal_close'
/usr/bin/ld: /tmp/ccKM74Fy.o: in function `terminal_print(int, int, char const*)':
main2.cpp:(.text._Z14terminal_printiiPKc[_Z14terminal_printiiPKc]+0x40): undefined reference to `terminal_print_ext8'
collect2: error: ld returned 1 exit status

I assumed these symbols would be contained in the file libBearLibTerminal.so, but apparently not.

I created the .so and .h by compiling this repository using cmake .; make

cfyzium commented 4 years ago

First, the -L (uppercase) option specifies an additional path for library search, similar to -I. It is the -l (lowercase) that specifies the name (without prefixes or suffixes) of a library to link with.

Second, the linker is very particular about link order, you have to specify dependencies after objects using them. In this case, -lBearLibTerminal should go after main2.cpp.

In other words (assuming both .h/.so are in the current directory) your command should look like

g++ -I. -L. main2.cpp -lBearLibTerminal -o app2.exe

Also note that by default Linux binaries do not search for dependencies besides themselves. So the compiled binary won't run failing to find the library it has just linked with a moment ago. Dependencies are supposed to go into system directories =/. To make things simpler, you can make it search the current directory by adding a few flags:

g++ -I. -L. -Wl,-rpath,. main2.cpp -lBearLibTerminal -o app2.exe