polPolpol
polPolpol

Reputation: 1

Do I need a Shared library in a CMAKE project with several subdirectories? How do I do it?

I need to implement a StatisticsLogger library on to a C++/C solution built with cmake and compiled with VS#2010. The project uses the ADTF API, since is built for the ADTF framework. The solution is composed by different modules/projects, which include their own CMakeLists.txt.

My doubt/problem is regarding the library and Cmake, I need it to be accesible to every single module, but it can't be copied, them all should access the same StatisticsLogger library, that I have implemented with a Singleton.

When I run the framework, the concurrent execution accesses StatsLogger constructor once on each module, like if I had created one StatsLogger on each module, making it unable to trace together all the data I want to log and difficulting file handling.

This is how I added the library in CMakeLists.txt:

add_library(loggerModule 
    ${DSTD_DIR}/dstdfloat.h
    ${DSTD_DIR}/dstdint.h
    ${DSTD_DIR}/dstdbool.h
    ${SUPT_DIR}/logg/statslogger.h
    ${SUPT_DIR}/logg/statslogger_c_connector.h
    ${SUPT_DIR}/logg/statslogger_c_connector.cpp
    ${SUPT_DIR}/logg/statslogger.cpp
)


#set_target_properties(loggerModule PROPERTIES VERSION ${PROJECT_VERSION})
#set_target_properties(loggerModule PROPERTIES PUBLIC_HEADER include/mylib.h)

link_libraries(loggerModule)

It would seem that adding SHARED property to the command add_library would do the job, but Im not capable of getting it working. It returns several link problems.

So, regarding my doubt, Is this the way to get the desired funcionality, to make the library SHARED? What am I doing wrong?

Main CMakeLists.txt:

# CMAKE for test filter build
cmake_minimum_required(VERSION 2.8.4 FATAL_ERROR)

# Set default install prefix
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})

# Set project name
project(${PROJECT_NAME})
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Environment checks
find_package(ADTF 2.14.2 REQUIRED)
IF (NOT DEFINED ADTF_FOUND)
    MESSAGE( FATAL_ERROR "ADTF was NOT found!!!")
ENDIF()

find_package(ADTF_DISPLAY_TOOLBOX REQUIRED)
IF (NOT DEFINED ADTF_DISPLAY_TOOLBOX_FOUND)
    MESSAGE( FATAL_ERROR "ADTF_DISPLAY_TOOLBOX was NOT found!!!")
ENDIF()

set(COMPLETE_PROJECT_BINARY_OUTPUT_DIR ${CMAKE_SOURCE_DIR}/${PROJECT_BINARY_OUTPUT_DIRECTORY})

# Set path to sw module dependencies
set(COMMON_DIR ${CMAKE_SOURCE_DIR}/common)
set(DSTD_DIR ${CMAKE_SOURCE_DIR}/common/dstd)
set(INTF_DIR ${CMAKE_SOURCE_DIR}/common/intf)
set(SUPT_DIR ${CMAKE_SOURCE_DIR}/common/supt)

#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)    
#option(BUILD_SHARED_LIBS "Build using shared libraries" ON)


include_directories(${DSTD_DIR})
include_directories(${SUPT_DIR}/logg)

add_library(loggerModule SHARED
    ${DSTD_DIR}/dstdfloat.h
    ${DSTD_DIR}/dstdint.h
    ${DSTD_DIR}/dstdbool.h
    ${SUPT_DIR}/logg/statslogger.h
    ${SUPT_DIR}/logg/statslogger_c_connector.h
    ${SUPT_DIR}/logg/statslogger_c_connector.cpp
    ${SUPT_DIR}/logg/statslogger.cpp
)


#set_target_properties(loggerModule PROPERTIES VERSION ${PROJECT_VERSION})
#set_target_properties(loggerModule PROPERTIES PUBLIC_HEADER include/mylib.h)

link_libraries(loggerModule)

# Set commands for BB nodes generation
# ...and dependencies for generation
# Go into sub-directory with filter sources

add_subdirectory(${CMAKE_SOURCE_DIR}/tool/adtf/af_acca)
#[33 other add_subdirectories commands]

Example subdirectory CMakeLists.txt:

# External required components have to be provided with path variables
# Internal required components have to be connected to the source file list

if(NOT DEFINED DSTD_DIR)
    message( FATAL_ERROR "AF_CDAS requires DSTD_DIR" )
endif()
if(NOT DEFINED INTF_DIR)
    message( FATAL_ERROR "AF_CDAS requires INTF_DIR" )
endif()
if(NOT DEFINED SUPT_DIR)
    message( FATAL_ERROR "AF_CDAS requires SUPT_DIR" )
endif()

set(CDAS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../)

include_directories(${COMMON_DIR})
[some more includes]

#
# CDAS FILES
#   
set(CDAS_FILES
        ${CDAS_DIR}/src/cdas/cdas.c
        ${CDAS_DIR}/src/cdas/cdas.h)
source_group("cdas\\files" FILES ${CDAS_FILES})

#
# CDAS ADTF WRAPPER
#   
set(CDAS_ADTF_WRAPPER
        ${CDAS_DIR}/tool/adtf/af_cdas/af_cdas_conf.h
        ${CDAS_DIR}/tool/adtf/af_cdas/af_cdas.h
        ${CDAS_DIR}/tool/adtf/af_cdas/af_cdas.cpp)
source_group("cdas\\adtf" FILES ${CDAS_ADTF_WRAPPER})

adtf_add_filter(CDAS
    # List source and header files of your filter and its required components
    # ${MAIN_FILES}
    ${CDAS_FILES}
    ${CDAS_ADTF_WRAPPER}
)

# stdafx.h workaround
if (MSVC)
    set_target_properties(CDAS PROPERTIES COMPILE_FLAGS "/Y-")
endif(MSVC)

# MANDATORY PATH SETTING !!!
install (TARGETS CDAS DESTINATION ${COMPLETE_PROJECT_BINARY_OUTPUT_DIR}/debug CONFIGURATIONS Debug)
install (TARGETS CDAS DESTINATION ${COMPLETE_PROJECT_BINARY_OUTPUT_DIR}/release CONFIGURATIONS Release)

adtf_set_folder(CDAS filter)

When using this files, I get the following error:

LINK : fatal error LNK1181: cannot open input file '......\Release\loggerModule.lib' [C:\Users\inno\Desktop\rad ar_processing\test\adtf\build\win64_vc100\tool\adtf\af_aoca\AOCA.vcxproj]

There isn't any loggerModule.lib in the Release folder, but a loggerModule.dll. In the Debug folder there is a .lib, but copying it to Release won't solve the problem.

I never got too familiar with Cmake, and I am trying to learn it, so I don't know what is going on here, if I am doing something wrong, or my approach wasn't good from the beggining.

Upvotes: 0

Views: 330

Answers (0)

Related Questions