Skip to content

Instantly share code, notes, and snippets.

@ritiek

ritiek/how_to.md

Last active May 7, 2019
Embed
What would you like to do?
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