Writing/Using C++ Libraries

狂风中的少年 提交于 2019-12-04 19:32:50

问题


I am looking for basic examples/tutorials on:

  1. How to write/compile libraries in C++ (.so files for Linux, .dll files for Windows).

  2. How to import and use those libraries in other code.


回答1:


The code

r.cc :

#include "t.h"

int main()
{
    f();
    return 0;
}

t.h :

void f();

t.cc :

#include<iostream>
#include "t.h"    

void f()
{
    std::cout << "OH HAI.  I'M F." << std::endl;
}

But how, how, how?!

~$ g++ -fpic -c t.cc          # get t.o
~$ g++ -shared -o t.so t.o    # get t.so
~$ export LD_LIBRARY_PATH="." # make sure t.so is found when dynamically linked
~$ g++ r.cc t.so              # get an executable

The export step is not needed if you install the shared library somewhere along the global library path.



来源:https://stackoverflow.com/questions/42770/writing-using-c-libraries

标签

工具导航Map