diff options
author | Robert Dailey <rcdailey@gmail.com> | 2018-02-05 21:43:25 (GMT) |
---|---|---|
committer | Robert Dailey <rcdailey@gmail.com> | 2018-02-23 14:53:17 (GMT) |
commit | 076a356cd1972a149898fdba77d1dd83981447b4 (patch) | |
tree | b0ea4574fba9ccb786b699973f94e611f2f70fd3 /Tests/CSharpLinkFromCxx | |
parent | ebf0a0827992be5cc03e530f9dc06bd1d05c1aa6 (diff) | |
download | CMake-076a356cd1972a149898fdba77d1dd83981447b4.zip CMake-076a356cd1972a149898fdba77d1dd83981447b4.tar.gz CMake-076a356cd1972a149898fdba77d1dd83981447b4.tar.bz2 |
VS: Support C# project references
When specifying a pure C# target in the `target_link_libraries()` call to
another C++ target, a `<ProjectReference>` was setup for it (we wanted this)
but also a corresponding `.lib` was added under `<AdditionalDependencies>`
(we didn't want this).
This change introduces a check that prevents `.lib` linker options from
being used when the corresponding target for that library is a C# target.
Fixes: #17678
Diffstat (limited to 'Tests/CSharpLinkFromCxx')
-rw-r--r-- | Tests/CSharpLinkFromCxx/.gitattributes | 1 | ||||
-rw-r--r-- | Tests/CSharpLinkFromCxx/CMakeLists.txt | 19 | ||||
-rw-r--r-- | Tests/CSharpLinkFromCxx/CSharpLinkFromCxx.cs | 16 | ||||
-rw-r--r-- | Tests/CSharpLinkFromCxx/UsefulCSharpClass.cs | 12 | ||||
-rw-r--r-- | Tests/CSharpLinkFromCxx/UsefulManagedCppClass.cpp | 15 | ||||
-rw-r--r-- | Tests/CSharpLinkFromCxx/UsefulManagedCppClass.hpp | 16 |
6 files changed, 79 insertions, 0 deletions
diff --git a/Tests/CSharpLinkFromCxx/.gitattributes b/Tests/CSharpLinkFromCxx/.gitattributes new file mode 100644 index 0000000..57a39049 --- /dev/null +++ b/Tests/CSharpLinkFromCxx/.gitattributes @@ -0,0 +1 @@ +UsefulManagedCppClass.* -format.clang-format diff --git a/Tests/CSharpLinkFromCxx/CMakeLists.txt b/Tests/CSharpLinkFromCxx/CMakeLists.txt new file mode 100644 index 0000000..9a1a993 --- /dev/null +++ b/Tests/CSharpLinkFromCxx/CMakeLists.txt @@ -0,0 +1,19 @@ +# Take a C# shared library and link it to a managed C++ shared library +cmake_minimum_required(VERSION 3.10) +project (CSharpLinkFromCxx CXX CSharp) + +add_library(CSharpLibrary SHARED UsefulCSharpClass.cs) + +# we have to change the default flags for the +# managed C++ project to build +string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) +string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) + +# The C# project is a dependency of the C++/CLI project +add_library(ManagedCppLibrary SHARED UsefulManagedCppClass.cpp UsefulManagedCppClass.hpp) +target_compile_options(ManagedCppLibrary PRIVATE "/clr") +target_link_libraries(ManagedCppLibrary PUBLIC CSharpLibrary) + +# Main executable for the test framework +add_executable(CSharpLinkFromCxx CSharpLinkFromCxx.cs) +target_link_libraries(CSharpLinkFromCxx PRIVATE ManagedCppLibrary) diff --git a/Tests/CSharpLinkFromCxx/CSharpLinkFromCxx.cs b/Tests/CSharpLinkFromCxx/CSharpLinkFromCxx.cs new file mode 100644 index 0000000..31a74eb --- /dev/null +++ b/Tests/CSharpLinkFromCxx/CSharpLinkFromCxx.cs @@ -0,0 +1,16 @@ +using System; +using CSharpLibrary; + +namespace CSharpLinkFromCxx +{ + internal class CSharpLinkFromCxx + { + public static void Main(string[] args) + { + Console.WriteLine("Starting test for CSharpLinkFromCxx"); + + var useful = new UsefulManagedCppClass(); + useful.RunTest(); + } + } +} diff --git a/Tests/CSharpLinkFromCxx/UsefulCSharpClass.cs b/Tests/CSharpLinkFromCxx/UsefulCSharpClass.cs new file mode 100644 index 0000000..749e57d --- /dev/null +++ b/Tests/CSharpLinkFromCxx/UsefulCSharpClass.cs @@ -0,0 +1,12 @@ +using System; + +namespace CSharpLibrary +{ + public class UsefulCSharpClass + { + public string GetSomethingUseful() + { + return "Something Useful"; + } + } +} diff --git a/Tests/CSharpLinkFromCxx/UsefulManagedCppClass.cpp b/Tests/CSharpLinkFromCxx/UsefulManagedCppClass.cpp new file mode 100644 index 0000000..9468812 --- /dev/null +++ b/Tests/CSharpLinkFromCxx/UsefulManagedCppClass.cpp @@ -0,0 +1,15 @@ +#include "UsefulManagedCppClass.hpp" + +namespace CSharpLibrary +{ + UsefulManagedCppClass::UsefulManagedCppClass() + { + auto useful = gcnew UsefulCSharpClass(); + m_usefulString = useful->GetSomethingUseful(); + } + + void UsefulManagedCppClass::RunTest() + { + Console::WriteLine("Printing from Managed CPP Class: " + m_usefulString); + } +} diff --git a/Tests/CSharpLinkFromCxx/UsefulManagedCppClass.hpp b/Tests/CSharpLinkFromCxx/UsefulManagedCppClass.hpp new file mode 100644 index 0000000..def7cea --- /dev/null +++ b/Tests/CSharpLinkFromCxx/UsefulManagedCppClass.hpp @@ -0,0 +1,16 @@ +using namespace System; + +namespace CSharpLibrary +{ + public ref class UsefulManagedCppClass + { + public: + + UsefulManagedCppClass(); + void RunTest(); + + private: + + String^ m_usefulString; + }; +} |