0

我知道这个话题已经讨论过不止一次了,但我不明白出了什么问题。我查找了几个网站,但提供的方法都不适合我。

我想用 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错误,每个项目都会发生`

还有一个警告:

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。

4

1 回答 1

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()

如您所见,我从全局 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 个而不是 100 个项目怎么办?单独修改每个 CMakeLists 真的很痛苦。

于 2013-09-13T09:56:51.307 回答