A
A
amorphine2017-09-24 20:56:03
C++ / C#
amorphine, 2017-09-24 20:56:03

Can the code (definition) in the header files be moved to a shared library?

There is such library
And declarations and definitions are in one file.
When trying to compile as shared, a so-file of about 7 kb is created, but with ldd it shows that the desired shared-library is not linked, and the size of the executable file says that the library is statically linked.
Do I understand correctly that the code in h-files is included and compiled together with the executable file, and in order to put it in shared, you need to:

  • create an intermediate cpp / h-dummy, where to include csv.h
  • rewrite h to h/cpp

cmake
cmake_minimum_required(VERSION 3.7.2)
project(csvp)

option(BUILD_SHARED_LIBS "build as shared library" ON)
set(CMAKE_CXX_STANDARD 11)

find_package (Threads)

include_directories(libs/tinyxml2)
include_directories(libs/tclap/include)
include_directories(libs/csv)

set(SOURCE_FILES main.cpp)
add_executable(csvp ${SOURCE_FILES})

add_subdirectory(libs/tinyxml2)

add_library(tclap SHARED libs/tclap/include/tclap/CmdLine.h)
set_target_properties(tclap PROPERTIES LINKER_LANGUAGE CXX)

add_library(csv SHARED libs/csv/csv.h)
set_target_properties(csv PROPERTIES LINKER_LANGUAGE CXX)

add_library(csvparser SHARED libs/csv-parser/csv_parser.cpp)
set_target_properties(csv PROPERTIES LINKER_LANGUAGE CXX)

target_link_libraries (csvp ${CMAKE_THREAD_LIBS_INIT} tinyxml2 csv tclap csvparser)

ldd
ldd csvp
        linux-vdso.so.1 =>  (0x00007fff8fd48000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f41922fb000)
        libtinyxml2d.so.5 => /var/www/pprc_csv2xml_cpp/cmake-build-debug/libs/tinyxml2/libtinyxml2d.so.5 (0x00007f41920de000)
        libcsvparser.so => /var/www/pprc_csv2xml_cpp/cmake-build-debug/libcsvparser.so (0x00007f4191ed9000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f4191b57000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4191940000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4191576000)
        /lib64/ld-linux-x86-64.so.2 (0x0000558538071000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f419126d000)

For example, I added a similar project to the assembly (I called the target csvparser, which has h / cpp), it compiles into a normal shared-lib, it can be seen from ldd.
For the first time I write on CPP, I just evaluate the entry threshold.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2017-09-25
@amorphine

It is necessary to make a CPP file into which to transfer as much code from the program as possible. And the code is generated by:
• Static- and global variables.
• Non-template, non-inline functions.
• Non-inline, fully debugged functions template<>.
• A non-extern type template declaration template class std::vector<int>.
Moreover, it is a mistake to keep these four things in headers - but, oddly enough, this is not in our library, because it completely relies on templates. But there are a couple of reserves.
1. The function in the class body automatically becomes inline, and, I would say, AsynchronousReader::initit would be a mistake to keep something like inline. In an amicable way, it is necessary in CPP. But this is quite a small part of the code.
2. You can also upload the most common versions of classes / functions to SO. To do this, they must be taken out of the class bodies and do something like this.

// H
template <int x>
void foo () { std::cout << x << std::endl; }

extern template void foo<2>();

// CPP
template void foo<2>();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question