0

The project structure of my static library is exactly the same as defined in this question and my CMakeList.txt is exactly the same:

cmake_minimum_required (VERSION 2.8) 
project (project_name) 

add_subdirectory (src) 
add_subdirectory (test) 

I want to build the test folder after my static library. I have also added add_test() but only problem is my test directory doesn't contain unit tests, but actual test application like following code:

#include <iostream>
main() {
 /* 
   this code uses static library functions in it. There is not Unit Test defined in it.
*/
}

add_test() didn't helped me, and I am still getting a cannot find -ldal error. dal is my static library name, which I have added as target_link_library to my test application.

libdal/src/CMakeList.txt

cmake_minimum_required(VERSION 3.12)
project(dal)

# Flags Variables
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")

# Include Directeries
include_directories(${CPP_KAFKA_INC})
include_directories(${CPP_MYSQL_INC})
include_directories(${CPP_ALTIBASE_INC})
include_directories(${AFN_STATE_INC})
include_directories(${AFN_DAL_INC})
include_directories(${AFN_COMMONS_INC})
include_directories(${AFN_CONFIG_INC})
include_directories(${SQL_ODBC_INC})
# Executable Target(s)
file(GLOB SOURCES *.cpp)
add_library(${PROJECT_NAME} ${SOURCES})

target_link_libraries(${PROJECT_NAME}
        ${AFN_LIB}
        "-lafinit_state"
        "-luuid"
        "-lz"
        "-lpthread"
        ${PQXX_LIB}
        ${PQ_LIB}
        "-lmysqlcppconn"
        ${libmscpp}
        ${kafkacpplib}
        ${rdkafkalib}
        ${rdkafkacpplib}
        afiniti_config $<$<CONFIG:DEBUG>:-lgcov>
        )

# Installation
install(CODE "MESSAGE(\"Installing ${PROJECT_NAME} \")")
install(TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib)

libdal/test/CMakeList.txt

cmake_minimum_required(VERSION 3.12)
project(testdal)

# Flags Variables
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17" )

option(DAL_PUSH_TESTER      "enable DAL push tester"    ON)

# Include Directeries
include_directories(${CPP_KAFKA_INC})
include_directories(${AFN_DAL_INC})
include_directories(${CPP_MYSQL_INC})
include_directories(${AFN_STATE_INC})
include_directories(${AFN_CONFIG_INC})

add_executable(${PROJECT_NAME}  main.cpp)
target_link_libraries(${PROJECT_NAME}
        ${AFN_LIB}
        "-lmysqlcppconn"
        "-luuid"
        "-lz"
        ${libmscpp}
        "-ldal"
        ${PQXX_LIB}
        ${PQ_LIB}
        ${kafkacpplib}
        ${rdkafkalib}
        ${rdkafkacpplib}
        "-lafinit_state"
        "-luuid"
        "-lpthread"
        afiniti_config $<$<CONFIG:DEBUG>:-lgcov>
        ${AFN_LICENSE_LIB} "-laf_license_manager"
        )
    # Installation
install(CODE "MESSAGE(\"Installing ${PROJECT_NAME} \")")
install(TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION bin/tests
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib)


if (DAL_PUSH_TESTER)
    add_executable(dal_push_tester dal_push_tester.cpp)
    target_link_libraries(dal_push_tester
            ${AFN_LIB}
            "-lmysqlcppconn"
            "-luuid"
            "-lz"
            ${libmscpp}
            "-ldal"
            ${PQXX_LIB}
            ${PQ_LIB}
            ${kafkacpplib}
            ${rdkafkalib}
            ${rdkafkacpplib}
            "-lafinit_state"
            "-luuid"
            "-lpthread"
            afiniti_config $<$<CONFIG:DEBUG>:-lgcov>
            ${AFN_LICENSE_LIB} "-laf_license_manager"
            )
# Installation
install(CODE "MESSAGE(\"Installing dal_push_tester  \")")
install(TARGETS dal_push_tester 
        RUNTIME DESTINATION bin/tests
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib)

endif ()

Kevin
  • 16,549
  • 8
  • 60
  • 74
Sahib Yar
  • 1,030
  • 11
  • 29

1 Answers1

2

The issue has been raised a couple times on the site, but there are some other issues with your CMake file that warrant explanation.

The problem is that the linker doesn't know where to find the dal library, because you've only provided "-ldal", without a full path to the library or anything. You can fix this by simply using the CMake library target name (dal) in target_link_libraries() instead, because it was defined earlier in the same CMake invocation.

target_link_libraries(${PROJECT_NAME}
        ${AFN_LIB}
        "-lmysqlcppconn"
        "-luuid"
        "-lz"
        ${libmscpp}
        dal              #   <------ Note the change on this line.
        ${PQXX_LIB}
        ${PQ_LIB}
        ${kafkacpplib}
        ${rdkafkalib}
        ${rdkafkacpplib}
        "-lafinit_state"
        "-luuid"
        "-lpthread"
        afiniti_config $<$<CONFIG:DEBUG>:-lgcov>
        ${AFN_LICENSE_LIB} "-laf_license_manager"
        )

Furthermore, target_link_libraries doesn't need the explicit -l link flag added; it adds this automatically. Also, you should always specify the scoping argument (e.g. PRIVATE, PUBLIC, etc.) for this and other target_* calls. Also, also, you have linked uuid twice, so you can probably remove one of these:

target_link_libraries(${PROJECT_NAME} PRIVATE
        ${AFN_LIB}
        mysqlcppconn
        uuid
        z
        ${libmscpp}
        dal
        ${PQXX_LIB}
        ${PQ_LIB}
        ${kafkacpplib}
        ${rdkafkalib}
        ${rdkafkacpplib}
        afinit_state
        pthread
        afiniti_config $<$<CONFIG:DEBUG>:gcov>
        ${AFN_LICENSE_LIB} af_license_manager
        )

In addition, the manual modification of CMAKE_CXX_FLAGS in this line:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")

is highly discouraged. For setting the C++ standard, you should use:

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

or one of the other more modern approaches discussed in this question.

I encourage you to browse through some of the content regarding modern CMake practices.

Kevin
  • 16,549
  • 8
  • 60
  • 74