When I generate the .vcxproj for my project with cmake 3.2.1, it generates the line <CompileAs>CompileAsCpp</CompileAs>
But when I try the cmake version 3.18.1, the generated .vcxproj file does not contain that previous line causing my tests to fail (the code compiles).
Here the cmake code which set the project properties :
add_library(${PROJECT_NAME} SHARED ${PROJECT_SRC})
if(CMAKE_BUILD_TOOL MATCHES "(msdev|devenv|nmake|MSBuild)")
target_compile_options(${PROJECT_NAME} PRIVATE "/TP")
endif()
target_include_directories(${PROJECT_NAME} PRIVATE ${PUBLIC_INCLUDES} ${PRIVATE_INCLUDES} )
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set_target_properties(${PROJECT_NAME} PROPERTIES PDB_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
How can I ask to cmake to generate that missing line (<CompileAs>CompileAsCpp</CompileAs>)?
Thank you.
I tried to use CMake 3.18.0 and it worked.
The CompileAs tag was generated so I will use this version instead of the CMake 3.18.1
Thank you.
Related
I get this error, which I've been trying to understand for some time now but getting nowhere.
add_library cannot create target "Plugin" because another target
with the same name already exists. The existing target is a shared library created
in source directory "D:/CHAI3D/SOFA/src/applications/plugins/plugin".
See documentation for policy CMP0002 for more details.
Below is the CMakelists.txt added for reference. I tried to remove the unnecessary code. So far, I've tried allowing duplicate targets with set(ALLOW_DUPLICATE_CUSTOM_TARGETS TRUE)
But to no avail
cmake_minimum_required(VERSION 3.1)
project(Plugin VERSION 21.06.99)
# Policies
cmake_policy(SET CMP0079 NEW)
set(ALLOW_DUPLICATE_CUSTOM_TARGETS TRUE)
set (PLUGIN_VERSION ${PROJECT_VERSION})
set(HEADER_FILES
src/initPlugin.h
...
)
set(SOURCE_FILES
src/initPlugin.cpp
...
)
file(GLOB_RECURSE RESOURCE_FILES "*.md" "*.psl" "*.py" "*.pyscn" "*.scn" "*.ah")
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${RESOURCE_FILES} )
target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>")
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-DSOFA_BUILD_PLUGIN")
target_link_libraries(${PROJECT_NAME} SofaCore SofaConstraint SofaSimpleFem SofaBaseMechanics SofaRigid SofaBaseVisual SofaOpenglVisual)
## Install rules for the library and headers; CMake package configurations files
sofa_create_package_with_targets(
PACKAGE_NAME ${PROJECT_NAME}
PACKAGE_VERSION ${PROJECT_VERSION}
TARGETS ${PROJECT_NAME} AUTO_SET_TARGET_PROPERTIES
INCLUDE_SOURCE_DIR "src"
INCLUDE_INSTALL_DIR ${PROJECT_NAME}
RELOCATABLE "plugins"
)
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(TARGET ${PROJECT_NAME} PROPERTY FOLDER "plugins")
Is it a bug of CMake, or may I have a mistake? Can anybody point out that how to fix the problem? Any help will be greatly appreciated.
First, I listed a third party target's dll files. I do believe that it is ok.
project(assimp)
add_library(${PROJECT_NAME} SHARED IMPORTED GLOBAL)
set(ASSIMP_ROOT ${CMAKE_CURRENT_LIST_DIR})
set(ASSIMP_NAME "assimp-vc142-mt")
set_target_properties(${PROJECT_NAME} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${ASSIMP_ROOT}/include"
IMPORTED_IMPLIB_RELEASE "${ASSIMP_ROOT}/lib/${ASSIMP_NAME}.lib"
IMPORTED_IMPLIB_DEBUG "${ASSIMP_ROOT}/lib/${ASSIMP_NAME}d.lib"
IMPORTED_LOCATION_RELEASE "${ASSIMP_ROOT}/bin/${ASSIMP_NAME}.dll"
IMPORTED_LOCATION_DEBUG "${ASSIMP_ROOT}/bin/${ASSIMP_NAME}d.dll")
Then, I wanted to use this lib, and the problem came.
cmake_minimum_required(VERSION 3.15)
include(cmake/Functions.cmake)
project(my_show_case)
add_subdirectory(thirdparty)
add_executable(${PROJECT_NAME})
file(GLOB_RECURSE srcs ${CMAKE_CURRENT_LIST_DIR}/include/*.h ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)
target_sources(${PROJECT_NAME} PUBLIC ${srcs})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/)
get_property(ASSIMP_DEBUG_LIB TARGET assimp PROPERTY IMPORTED_LOCATION_DEBUG)
get_property(ASSIMP_RELEASE_LIB TARGET assimp PROPERTY IMPORTED_LOCATION_RELEASE)
# This section would get a wrong file name which has a '>' suffix
set(MY_Lib $<$<CONFIG:Debug>:${ASSIMP_DEBUG_LIB}>$<$<CONFIG:Release>:${ASSIMP_RELEASE_LIB}>)
get_filename_component(FILENAME "${MY_Lib}" NAME)
# prints: "assimp-vc142-mt.dll>"
file(GENERATE OUTPUT TestOut.txt CONTENT "${FILENAME}")
The file TestOut.txt would print "assimp-vc142-mt.dll>", as you can see, it has a suffix ">".
I am trying to link the psapi library to a project with cmake, nothing complex. Here's my cmake-file:
cmake_minimum_required(VERSION 2.8)
project(BenchmarkTests)
add_definitions(-DPSAPI_VERSION=1)
if (WIN32)
FILE(GLOB win32_head
Timer.h
win_Memory.h
win_Processor.h
BenchmarkTests.h)
FILE(GLOB win32_source *.cpp)
SET(win32_test ${win32_head} ${win32_source})
SET(LIBDIR_NAME "C:/Program Files (x86)/Windows Kits/8.1/Lib/winv6.3/um/x64/")
SET(LIBDIR $ENV{${LIBDIR_NAME}})
SET(LIBNAME "Psapi.Lib")
find_library (Psapi ${LIBNAME} {LIBDIR})
ADD_EXECUTABLE(bmTests ${win32_test})
TARGET_LINK_LIBRARIES(bmTests Psapi)
SOURCE_GROUP("win32" FILES ${win32_test})
endif()
There are no other "Psapi.Lib" files on my computer except for in ".../um/x86", but my system is 64-bit so I want the x64, no? Anyhow the output in CMake GUI for Psapi field is "Psapi-NOTFOUND" and in VS2013 all of the functions in Psapi.h recieve syntax errors. I guess since they can't link to the library. Am I forgetting something vital in my cmake file? Any suggested fix or alternative method is welcome, thanks in advance.
I get the same result when I try the below instead of find_library(...)
add_library(Psapi STATIC IMPORTED)
set_property(TARGET Psapi PROPERTY IMPORTED_LOCATION "C:/Program Files (x86)/Windows Kits/8.1/Lib/winv6.3/um/x64/Psapi.Lib")
For future reference I got it to work in CMake as follows, credit goes to Chibueze Opata on this question:
find_library (PSAPI Psapi)
...
add_executable(...)
...
target_link_libraries(Basic -lpsapi)
I have a custom target that is in fact an externally generated library that I want to integrate in my build.
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/liblib2.a
COMMAND make -f ${CMAKE_CURRENT_SOURCE_DIR}/makefile liblib2.a)
add_custom_target(lib2
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/liblib2.a)
How can I tell cmake that this target is in fact a library, where it can be found and where are the headers ?
To be clear : I don't want the upper CMakeList using this library having to manually specify include folders and the library location folder It must be done automatically (from the target properties).
On a standard cmake library I would just have to add the INTERFACE_INCLUDE_DIRECTORIES property in the library CMakeLists to make cmake link my app with the relevant -I and -L gcc parameters :
set_target_properties(lib1
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES
${CMAKE_CURRENT_SOURCE_DIR})
But in the case of a custom target I don't know how to to it.
Any clue ?
Thanks for your help.
Thanks to zaufi it works!
For others who may be interested in embedded externally build target inside cmake here is what I did :
cmake_minimum_required(VERSION 2.8)
SET(LIB_FILE ${CMAKE_CURRENT_SOURCE_DIR}/bin/liblib2.a)
SET(LIB_HEADER_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/include)
# how to build the result of the library
add_custom_command(OUTPUT ${LIB_FILE}
COMMAND make
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
# create a target out of the library compilation result
add_custom_target(lib2_target DEPENDS ${LIB_FILE})
# create an library target out of the library compilation result
add_library(lib2 STATIC IMPORTED GLOBAL)
add_dependencies(lib2 lib2_target)
# specify where the library is and where to find the headers
set_target_properties(lib2
PROPERTIES
IMPORTED_LOCATION ${LIB_FILE}
INTERFACE_INCLUDE_DIRECTORIES ${LIB_HEADER_FOLDER})
Now in a CMakeLists.txt I can do somthing like
add_subdirectory(${ROOT_DIR}/lib1 bin/lib1)
add_subdirectory(${ROOT_DIR}/lib2 bin/lib2)
add_executable(app app.c )
target_link_libraries(app lib1 lib2)
No need to specify where the .a and the .h are.
You can use add_library() and tell that it actually imported. Then, using set_target_properties() you can set required INTERFACE_XXX properties for it. After that, you can use it as an ordinal target like every other built by your project.
Thank you for posting the solution. I have wrapped your snippet in a function:
function(add_external_library)
set(options)
set(oneValueArgs TARGET WORKING_DIRECTORY OUTPUT COMMENT)
set(multiValueArgs COMMAND INCLUDE_DIRS)
cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" ${multiValueArgs}" ${ARGN})
# Specify how to build the result of the library
add_custom_command(OUTPUT "${ARGS_OUTPUT}"
COMMAND ${ARGS_COMMAND}
WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}"
COMMENT "${ARGS_COMMENT}")
# Create a target out of the library compilation result
add_custom_target(${ARGS_TARGET}_target DEPENDS ${ARGS_OUTPUT})
# Create an library target out of the library compilation result
add_library(${ARGS_TARGET} STATIC IMPORTED GLOBAL)
add_dependencies(${ARGS_TARGET} ${ARGS_TARGET}_target)
# Specify where the library is and where to find the headers
set_target_properties(${ARGS_TARGET}
PROPERTIES
IMPORTED_LOCATION "${ARGS_OUTPUT}"
INTERFACE_INCLUDE_DIRECTORIES "${ARGS_INCLUDE_DIRS}")
endfunction()
# Example
add_external_library(TARGET YourLib
COMMAND /bin/bash compile_your_lib.sh
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT "output/yourlib.a"
INCLUDE_DIRS "include/a" "include/b"
COMMENT "Building YourLib")
add_executable(YourExe)
target_link_libraries(YourExe YourLib)
I am trying to add xapian search engine library in cmake file
project(search)
cmake_minimum_required(VERSION 2.8)
find_package(Xapian REQUIRED)
aux_source_directory(. SRC_LIST)
target_link_libraries(${PROJECT_NAME}
${Xapian_LIBRARY}
)
add_executable(${PROJECT_NAME} ${SRC_LIST})
This is not working can any one tell me how to add this if i compile with -lxapian it works
Swap target_link_libraries() and add_executable() calls. You can link library only to already defined target.
And use ${XAPIAN_LIBRARIES} instead of ${Xapian_LIBRARY}.