1
votes

I start to use CMake recently and I starting to get used to the program. I managed to generate two Visual Studio solutions/projects: a shared library (core.dll) and a static library (zlib.lib). Now I still have a problem, my core.dll library makes references to the zlib library in Visual Studio. (In Properties -> Common Properties -> References). How can I reproduce this?

Here is how my files are located:

project
---core
------src
------CMakeLists.txt
---external
------zlib
------CMakeLists.txt
---------zlib-1.2.8

And here my two CMakeLists :

For core.dll

cmake_minimum_required(VERSION 2.8)

#Configuration project

project(core CXX)

#add definitions
#set(LIBRARY_OUTPUT_PATH_DEBUG vc2013_x64/bin/)
set(BIN_PATH "../vc2013_x64d")
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
add_definitions(-D_UNICODE -D_USRDLL -DCORE_EXPORTS)
add_definitions("/Gm")#"/GL" "/Gy" 

#remove_definitions
add_definitions(-UCMAKE_INTDIR="Release")
#Configuration Library
add_library(

        core

        SHARED

    ... .h and .cpp 
)

For zlib.lib

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

#Configuration de ZLib

PROJECT(zlib C)

SET(BIN_PATH "../core/vc2013_x64d")
SET_TARGET_PROPERTIES(PROPERTIES LINKER_LANGUAGE C)
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
SET(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /W3")
ADD_DEFINITIONS("/Gm" -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_UNICODE)


ADD_LIBRARY(

    zlib

    STATIC

    ... .h and .c

)

EDIT : It's work like this :

cmake_minimum_required(VERSION 2.8)

#Configuration de Zlib.lib

PROJECT(zlib C)

SET(BIN_PATH "../cmakex64d")
#SET_TARGET_PROPERTIES(PROPERTIES LINKER_LANGUAGE C)
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
SET(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /W3")
ADD_DEFINITIONS("/Gm" -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_UNICODE)


ADD_LIBRARY(

    zlib

    STATIC

    external/zlib/zlib-1.2.8/adler32.c
    external/zlib/zlib-1.2.8/compress.c
    external/zlib/zlib-1.2.8/crc32.c
    external/zlib/zlib-1.2.8/deflate.c
    external/zlib/zlib-1.2.8/gzclose.c
    external/zlib/zlib-1.2.8/gzlib.c
    external/zlib/zlib-1.2.8/gzread.c
    external/zlib/zlib-1.2.8/gzwrite.c
    external/zlib/zlib-1.2.8/infback.c
    external/zlib/zlib-1.2.8/inffast.c
    external/zlib/zlib-1.2.8/inflate.c
    external/zlib/zlib-1.2.8/inftrees.c
    external/zlib/zlib-1.2.8/trees.c
    external/zlib/zlib-1.2.8/uncompr.c
    external/zlib/zlib-1.2.8/zutil.c

    external/zlib/zlib-1.2.8/crc32.h
    external/zlib/zlib-1.2.8/deflate.h
    external/zlib/zlib-1.2.8/gzguts.h
    external/zlib/zlib-1.2.8/inffast.h
    external/zlib/zlib-1.2.8/inffixed.h
    external/zlib/zlib-1.2.8/inflate.h
    external/zlib/zlib-1.2.8/inftrees.h
    external/zlib/zlib-1.2.8/trees.h
    external/zlib/zlib-1.2.8/zlib.h
    external/zlib/zlib-1.2.8/zutil.h

)

project(core CXX)

set(BIN_PATH "../cmakex64d")
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
set_property(GLOBAL PROPERTY LINK_LIBRARY_DEPENDENCIES "yes")
add_definitions(-D_UNICODE -D_USRDLL -DCORE_EXPORTS)
add_definitions("/Gm")

#Configuration de core.dll
add_library(

        core

        SHARED

    core/src/asserts.h
    core/src/errors.h
    core/src/filepath.h
    core/src/memory.h
    core/src/resources.h
    core/src/stdafx.h
    core/src/streams.h
    core/src/string.h
    core/src/system.h
    core/src/variants.h

    core/src/filepath.cpp
    core/src/main.cpp
    core/src/memory.cpp
    core/src/resources.cpp
    core/src/stdafx.cpp
    core/src/streams.cpp
    core/src/string.cpp
    core/src/system.cpp     
)
link_directories("/build/lib/")
target_link_libraries(core zlib)

But when i "configure" in CMake i've this warning now :

CMake Warning (dev) at C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/CMakeDetermineCXXCompiler.cmake:106 (if): Policy CMP0054 is not set: Only interpret if() arguments as variables or keywords when unquoted. Run "cmake --help-policy CMP0054" for policy details. Use the cmake_policy command to set the policy and suppress this warning.

Quoted variables like "MSVC" will no longer be dereferenced when the policy is set to NEW. Since the policy is not set the OLD behavior will be used. Call Stack (most recent call first): CMakeLists.txt:51 (project) This warning is for project developers. Use -Wno-dev to suppress it.

CMake Warning (dev) at C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/CMakeFindBinUtils.cmake:33 (if):
Policy CMP0054 is not set: Only interpret if() arguments as variables or keywords when unquoted. Run "cmake --help-policy CMP0054" for policy details. Use the cmake_policy command to set the policy and suppress this warning.

Quoted variables like "MSVC" will no longer be dereferenced when the policy is set to NEW. Since the policy is not set the OLD behavior will be used. Call Stack (most recent call first): C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/CMakeDetermineCXXCompiler.cmake:162 (include) CMakeLists.txt:51 (project) This warning is for project developers. Use -Wno-dev to suppress it.

Why ? I've not that before ?

You shouldn't link shared-object libs with static libs, they should be static or shared to avoid problems. Then you could try to use link_directories command before using target_link_libraries(core zlib) - fenix688
@fenix688 Ok. Sorry but I'm confused ... i've I have to add link_directory("../external") to my end of CMakeLists for Core ? and after target_link_libraries(core zlib) ? I've tried this but it's doesn't seems to work. Can you be more specific please ? - Algorys
As you compile zlib, adding target_link_libraries(core zlib) will tell cmake to add a link dependency to core, so it will find the link directory by itself. - Gerald
ok that's works, but i have to make one CMakeLists for the 2 projects. That's an error from me... sorry. - Algorys