Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
edbird opened this issue Jul 19, 2020 · 1 comment
Open

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

edbird opened this issue Jul 19, 2020 · 1 comment

Comments

@edbird
Copy link

edbird commented Jul 19, 2020

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
Copy link
Owner

cfyzium commented Jul 20, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants