Skip to content

Instantly share code, notes, and snippets.



Last active May 7, 2019
What would you like to do?
Compiling source with static and shared library
// bin_hello.cpp

#include "lib_hello.h"

int main() {
// 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
$ g++ bin_hello.cpp -o hello_world
$ LD_LIBRARY_PATH=$(pwd) ./hello_world
Hello World!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment