CMake Target_Link_Libraries失败

时间:2013-09-12 13:45:56

标签: visual-studio-2010 cmake

我知道这个话题已经被讨论了不止一次,但我不明白出了什么问题。我查了几个网站,但没有提供的方法对我有用。

我想用CMake创建一个包含10个项目的VS解决方案。 每个项目必须链接myLibrary.lib

#CMakeLists.txt:

cmake_minimum_required (VERSION 2.8)
project (Project_Example) 

SET(SRC_DIR_PATH C++)

INCLUDE_DIRECTORIES( "inc" )                          # OK, tested and works
LINK_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/lib)     # OK, tested and works
ADD_EXECUTABLE(${BINNAME} ${SRCS})                    # ?No idea?
TARGET_LINK_LIBRARIES(${BINNAME} myLibrary.lib)       # fails

ADD_SUBDIRECTORY( src/Project_01 )
IF( TRUE )
ADD_SUBDIRECTORY( src/Project_02 )
ADD_SUBDIRECTORY( src/Project_03 )
ADD_SUBDIRECTORY( src/Project_04 )
ADD_SUBDIRECTORY( src/Project_05 )
ADD_SUBDIRECTORY( src/Project_06 )
ADD_SUBDIRECTORY( src/Project_07 )
ADD_SUBDIRECTORY( src/Project_08 )
ADD_SUBDIRECTORY( src/Project_09 )
ADD_SUBDIRECTORY( src/Project_10 )
ENDIF()

每个项目都有自己的CMakeLists.txt,如下所示:

cmake_minimum_required (VERSION 2.8)

MESSAGE( STATUS "## ${CMAKE_CURRENT_BINARY_DIR} ##" )

IF ( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )
  ### Windows ###
  SET( LIB_PROJECT myLibrary )
  SET( LIB_SYSTEM  )
ELSEIF ( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" )
  ### Linux ###
  SET( LIB_PROJECT myLibrary dl )
  SET( LIB_SYSTEM /usr/local/lib )
ENDIF ( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )


SET( SOURCE_WITHOUT_EXT Project_01 )
SET( PROJECT_EXAMPLE ${SOURCE_WITHOUT_EXT} )

SET( SOURCE_FILES
  ${SOURCE_WITHOUT_EXT}.cpp
)


FOREACH( Librarys ${LIB_PROJECT} )
  MESSAGE("#### Librarys:${Librarys} next will find ...")
  SET( SHAREDLIB ${SHAREDLIB}-NOTFOUND )
  FIND_LIBRARY ( SHAREDLIB
    NAMES ${Librarys}
    PATHS ${PROJECT_BINARY_DIR}/lib ${LIB_SYSTEM}
    )
  MESSAGE("#### SHAREDLIB:${SHAREDLIB}")
  IF( SHAREDLIB )
    SET( LIBS_ALL ${LIBS_ALL} ${SHAREDLIB} )
    SET( SHAREDLIB ${SHAREDLIB}-NOTFOUND )
  ENDIF()
  MESSAGE("#### ${LIBRARY} found LIBS_ALL${LIBS_ALL}")
ENDFOREACH( Librarys )


ADD_EXECUTABLE( ${PROJECT_EXAMPLE} ${SOURCE_FILES} ) 

TARGET_LINK_LIBRARIES( ${PROJECT_EXAMPLE} ${LIBS_ALL})

这些项目的CMakeLists长期使用,因此被认为是好的。将所有项目集成到一个CMake解决方案时出现问题。

我得到的错误是:`“SHAREDLIB:SHAREDLIB-NOTFOUND”

cmake error, occurs for each project`

还有一个警告:

CMake Warning (dev) at CMakeLists.txt: (TARGET_LINK_LIBRARIES): 
Cannot specify link libraries for target "MyLibrary.lib" which is not build by this project.

CMake does not support this but it is used to work accidentally ans is being allowed for compatibility.

Policy CMP0016 is not set: target_link_libraries() reports error if only argument is not a target. Run "cmake --help-policy CMP0016" for policy details. Use the cmake_policy command to set the policy and suppress this warning.

所以,我想再次将此库添加到此解决方案中的任何项目中。

我的文件夹类似于:

C++\
     inc\    -> header files
     lib\    -> libraries
     src\    -> sources with 10 different folders of c++ files

CMake命令是:

cmake -G "Visual Studio 10" --build .. -DWIN_64:BOOL=OFF

我正在使用CMake 2.8和VS 2010。

1 个答案:

答案 0 :(得分:0)

问题的解决方案如下:

#CMakeLists.txt global:

cmake_minimum_required (VERSION 2.8)
project (Project_Example) 

SET(SRC_DIR_PATH C++)


ADD_SUBDIRECTORY( src/Project_01 )
IF( TRUE )
ADD_SUBDIRECTORY( src/Project_02 )
ADD_SUBDIRECTORY( src/Project_03 )
ADD_SUBDIRECTORY( src/Project_04 )
ADD_SUBDIRECTORY( src/Project_05 )
ADD_SUBDIRECTORY( src/Project_06 )
ADD_SUBDIRECTORY( src/Project_07 )
ADD_SUBDIRECTORY( src/Project_08 )
ADD_SUBDIRECTORY( src/Project_09 )
ADD_SUBDIRECTORY( src/Project_10 )
ENDIF()

如您所见,我从golbal CMakeLists中删除了Include和Link指令。相反,我将它们放入适当的CMakeLists文件中:

#CMakeLists.txt local (for each project):   

cmake_minimum_required (VERSION 2.8)        

MESSAGE( STATUS "## ${CMAKE_CURRENT_BINARY_DIR} ##" )    

SET(CMAKE_PROJECTNAME Project_01)    
SET(PROJECT_BINARY_DIR ../..)    

INCLUDE_DIRECTORIES( "${PROJECT_BINARY_DIR}/inc" )    


IF ( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )     
   ### Windows ###      
   SET( LIB_PROJECT MyLibrary )      
   SET( LIB_SYSTEM  )    
ELSEIF ( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" )      
   ### Linux ###
  SET( LIB_PROJECT MyLibrary dl )      
   SET( LIB_SYSTEM /usr/local/lib/ )    
ENDIF ( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )

SET( SOURCE_WITHOUT_EXT Project_01 )    
SET( PROJECT_EXAMPLE ${SOURCE_WITHOUT_EXT} )    
SET( SOURCE_FILES
  ${SOURCE_WITHOUT_EXT}.cpp
)

FOREACH( Librarys ${LIB_PROJECT} )      
   MESSAGE("#### Librarys:${Librarys} next will find ...")      
   SET( SHAREDLIB ${SHAREDLIB}-NOTFOUND )      
   FIND_LIBRARY ( SHAREDLIB
    NAMES ${Librarys}
    PATHS ${PROJECT_BINARY_DIR}/lib ${LIB_SYSTEM}
    )

   MESSAGE("#### SHAREDLIB:${SHAREDLIB}")      
   IF( SHAREDLIB )        
      SET( LIBS_ALL ${LIBS_ALL} ${SHAREDLIB} )        
      SET( SHAREDLIB ${SHAREDLIB}-NOTFOUND )      
      ENDIF()
  MESSAGE("#### ${LIBRARY} found LIBS_ALL${LIBS_ALL}")    
ENDFOREACH( Librarys )

ADD_EXECUTABLE( ${PROJECT_EXAMPLE} ${SOURCE_FILES} ) 

TARGET_LINK_LIBRARIES( ${PROJECT_EXAMPLE} ${LIBS_ALL})

虽然现在有效,但我仍然问自己,如果我没有10个项目,那该怎么办?这对于单独修改每个CMakeLists非常痛苦。