简体   繁体   中英

add_library is not working in CMake for adding CSharp source files?

I am trying to build the CSharp DLL from the set of .cs files. I used the add_library() function to add the source files ( .cs files). But it is giving warnings like "add_library for library libname without any source files". Please provide a solution to make it to work.

Thanks in advance.

CMakeLists.txt is:

cmake_minimum_required(VERSION 2.8)

project(MyAddIn)

AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/MyAdd-In/ MyAddIn_SOURCES)     


SET_SOURCE_FILES_PROPERTIES(${MyAddIn_SOURCES} PROPERTIES LANGUAGE CSharp)

ADD_LIBRARY(AddInTrg SHARED ${MyAddIn_SOURCES})

SET_TARGET_PROPERTIES(AddInTrg PROPERTIES 
                               LINKER_LANGUAGE CSharp
                               RUNTIME_OUTPUT_DIRECTORY bin
                               RUNTIME_OUTPUT_DIRECTORY_DEBUG bin
                               RUNTIME_OUTPUT_NAME MyAddIn_Bin
                               ARCHIVE_OUTPUT_DIRECTORY lib
                               ARCHIVE_OUTPUT_DIRECTORY_DEBUG lib
                               OUTPUT_NAME MyAddIn_Bin)

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/bin/MyAddIn_Bin.dll
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/MyAdd-In
                   COMMAND C:/WINDOWS/Microsoft.NET/Framework/v3.5/csc.exe
                   ARGS 
                   -target:library                 
                   -out ${CMAKE_CURRENT_BINARY_DIR}/bin/MyAddIn_Bin.dll
                   "${CMAKE_CURRENT_SOURCE_DIR}/MyAdd-In/Ribbon.cs" "${CMAKE_CURRENT_SOURCE_DIR}/MyAdd-In/ThisAddIn.cs" "${CMAKE_CURRENT_SOURCE_DIR}/MyAdd-In/Ribbon.xml"
                   COMMENT "-- Generating AddIn Dll")

As of CMake 3.8 , CMake now fully supports C# as a language. You should now be able to create C# assembly targets (DLLs) using something like the following:

# Define the DLL target, including all relevant project files.
add_library(AddInTrg SHARED ${MyAddIn_SOURCES})

# Set the .NET Framework version for the target.
set_property(TARGET AddInTrg PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
# Set the C# language version (defaults to 3.0 if not set).
set(CMAKE_CSharp_FLAGS "/langversion:6")
# Add in some .NET reference libraries.
set_property(TARGET AddInTrg PROPERTY VS_DOTNET_REFERENCES
    "Microsoft.CSharp"
    "System"
    "System.Core"
    "System.Data"
    "System.Deployment"
    "System.Drawing"
    "System.Net.Http"
    "System.Xml"
)

You can also use CMake to specify the .NET and C# versions, and to pull in any requisite .NET references.

Note: This support only encompasses Visual Studio 2010 and greater.

CMake currently has no language support for C#.

In particular, the add_library command will not work with .cs source files. You could attempt to handle all of the compilation manually using custom targets, but be aware that this is extremely difficult to pull off and the gains are limited.

Also, take a look at CMake's ExternelProject_Add . This might be the right choice if all you want is to build an external .csproj using MSBuild from within a bigger CMake environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM