Static Library

Here is our project. Build MeshLib to a static library file to be used in widget.

Source code files in MeshLib:

MeshAnalyzer.h

#pragma once

class MeshAnalyzer
{
public:
    MeshAnalyzer();
};

MeshAnalyzer.cpp

#include "MeshAnalyzer.h"
#include "Log.hpp"

MeshAnalyzer::MeshAnalyzer()
{
    Log( IInfo, ">> start to work! <<" );
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(MeshLib)

set( SRC src )

file(GLOB HEADER_FILES
    ${SRC}/*.h
    ${SRC}/*.hpp
    )
file(GLOB CXX_FILES
    ${SRC}/*.cxx
    ${SRC}/*.cpp
    )

include_directories(  ${CMAKE_CURRENT_SOURCE_DIR} )

add_library(${PROJECT_NAME} STATIC ${HEADER_FILES} ${CXX_FILES})


# install header files in #{CMAKE_INSTALL_PREFIX}/header
install(
    FILES ${HEADER_FILES}
    DESTINATION header
)

# install lib files in #{CMAKE_INSTALL_PREFIX}/lib
install(
    TARGETS ${PROJECT_NAME}
    ARCHIVE DESTINATION lib
)

Key files in UI project:

Widget.cpp

#include "MeshAnalyzer.h"

void Widget::on_pushButton_clicked()
{
    MeshAnalyzer obj;
}

Configures in CMakeLists.txt

# find lib's headers and library file.
include_directories(${CMAKE_INSTALL_PREFIX}/header)
link_directories(${CMAKE_INSTALL_PREFIX}/lib)

#...

target_link_libraries( ${PROJECT_NAME} MeshLib)

We need to set CMAKE_INSTALL_PREFIX for CMake and install headers and lib file to the path.

Run the exe to see the result:

Dynamic Library

Change CMakeLists.txt to generate dynamic library and copy files to exe path and library path.

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${CXX_FILES})

#...

# install lib files in #{CMAKE_INSTALL_PREFIX}/lib
install(
    TARGETS ${PROJECT_NAME}
    LIBRARY DESTINATION lib    # .so/.dll
    ARCHIVE DESTINATION lib    # lib
    RUNTIME DESTINATION lib    # Windows DLL
)

#copy dll to exe path, #{CMAKE_INSTALL_PREFIX}/../build/Release/
install(
    TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION ../build/Release/
)

Change declaration for MeshAnalyzer to export it.

#include "MacroDef.h"

class MYLIB_API MeshAnalyzer
{
//...

MacroDef.h :

#pragma once

#ifdef _WIN32
    #define MYLIB_API __declspec(dllexport)
#else
    #define MYLIB_API __attribute__((visibility("default")))
#endif


0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
0
Would love your thoughts, please comment.x
()
x