Ad 1: You can compile the .h file (you can explicitly override the language detection), but you do not want to do this. The .h file is intended to be included and will not compile anything useful to it.
Announcement 2: you need to link to the created library by passing -lmy_code (but note that for this you need to create it as libmy_code.so ) along with the corresponding -L flag (the directory where you put libmy_code.so ) in the linker. Like this:
g++ test.cpp -L. -lmy_code -ldl -o test
But you must also change the first command to:
g++ -fPIC -shared my_code.cpp -o libmy_code.so ^^^ libraries *must* have `lib` prefix on unix systems.
and this assumes that they both run in the same directory - if not, you must configure the -L option to point to the directory where libmy_code.so is located. You also need to place libmy_code.so somewhere where the dynamic linker can find it. Either set it, or set the environment variable LD_LIBRARY_PATH in the appropriate directory. Alternatively, you can compile with
g++ test.cpp my_code.so -ldl -o test
This does not force the lib prefix, and it creates the "rpath" entry in binary format, so it will find the library in its original location.
All of this assumes that you want to use it as a regular library, in which case you do not want to use dlopen . dlopen designed to open libraries as plug-ins at runtime, and can only be accessed by binding pointers to characters using dlsym() , but if you want to access the library in normal mode, you need to link it so that the linker could allow characters.
If instead you wanted to use dlopen , you should not include my_code.h in test.cpp and should not use everything that it defines, except for receiving characters with dlsym . And since it is C ++, this, in turn, requires an understanding of the character conversion scheme, because dlsym will not do this for you.