Skip to content

Instantly share code, notes, and snippets.

@ritiek
Last active May 7, 2019 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritiek/6fc774c4fd69ee411c52ad42103f4a7b to your computer and use it in GitHub Desktop.
Save ritiek/6fc774c4fd69ee411c52ad42103f4a7b to your computer and use it in GitHub Desktop.
Compiling source with static and shared library
// 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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment