// bin_hello.cpp
#include "lib_hello.h"
int main() {
hello_world();
}
// lib_hello.cpp
#include <iostream>
void hello_world() {
std::cout << "Hello World!";
}
// lib_hello.h
extern void hello_world();
- Compiling with static library:
$ g++ bin_hello.cpp lib_hello.cpp -o hello_world
$ ./hello_world
Hello World!
- Compiling with shared library:
$ g++ -shared -fPIC lib_hello.cpp -o lib_hello.so
$ g++ bin_hello.cpp lib_hello.so -o hello_world
$ LD_LIBRARY_PATH=$(pwd) ./hello_world
Hello World!