I am trying to set my settings in Visual Studio such that I can write managed C++ code to be called later by a C# program. I start with a new C++ project and here are the settings that I have changed and why:
Precompiled Headers to not using precompiled headers. As I just would rather not.
Common Language Runtime Support to /clr. As I understand it this is fundamentally needed.
Debugging Information Format from /ZI to none. As /clr and /ZI are incompatible
Enable minimal rebuild to no. As /clr and /Gm are incompatible.
Basic runtime checks to default. As /clr and /RTC1 are incompatible.
After all this I now come across many C2760 error codes located in xlocale or xlocnum. The error specifically states
syntax error: unexpected token 'identifier', expected 'catch'
Now, I am an extreme beginner here so maybe this is trivial and if so I apologize. My goal is to write a simple c++ class, wrap it with managed code, and have a DLL that I can link with a c# program later.
When I look at the MSDN Documentation page, all they say is to compile with /clr and /LD. Well there is no /LD option that I am able to find either, and I know that the /LD option is to create the DLL which is important. Any help or guidance would be greatly appreciated.
P.S. This is Visual Studio Community 2017
Truth is: you have to have 2 projects.
Just old good C/C++ project. You may put a 3rd-party library here like OpenSSL or any other cross-platform source. Here you may have some challenge to remove/wrap any platform-specific code.
Windows Runtime Component - this is exactly CLI code that acts like bridge between native C/C++ and managed C#. You can find this at File -> New project, then Visual C++ -> Windows Universal. General idea about the Runtime Component is you can seamlessly import CLI methods by signature into C# code, you can call C# callbacks right at the CLI code, all C#/CLI types are binary-compatible etc. This is why you can not "just add specially configured C/C++ project".
Related
I am trying to use the Console::SetCursorPosition(int, int) method. When I add the line using namespace System;, as shown in the C++ example from the preceding MSDN documentation, I get the error "Error: name must be a namespace name". I have been trying stuff for a couple hours now, but frustratingly without success. I have come across a ton of documentation for Visual Studio 2010 and 2012, but very little for 2013. The closest I have come is Lib Files as Linker Input. Steps 1-3 are easy enough, but step 4 is not clear to me: "Modify the Additional Dependencies property.". Looking at what is already there, it seems like I can just add a .lib file. But I don't have a System.lib.
So frustrated, so confused.
How can I use the System namespace in Visual Studio 2013 Update 4 for C++?
To formalize and expand on my comment, the Console class and generally the System namespace are part of the .NET framework.
In that context, the "C++" tab included in the MSDN documentation page of the Console::SetCursorPosition(int, int) method actually refers to the C++/CLI language. The C++/CLI language is distinct (although intentionally similar) from the C++ language. Correspondingly, the C++/CLI language contains various constructs which are not recognized by the C++ compiler toolset used when compiling Win32 projects.
In other words, to get rid of the "Error: name must be a namespace name" error, you would need to convert your Win32 C++ project to a CLR project. The easiest way to do that would be to create a new project, selecting one of the templates under "Visual C++" / "CLR":
The equivalent of .lib file depdendencies (relative to your Lib Files as Linker Input link) of Win32 projects for CLR project would be assembly references. You'd then typically add those assembly references with "Add References" under "Common Properties , References" project properties:
However, in your specific case you may very well find out that the System assembly reference is already included as part of the CLR project template.
You may want to check How to: Add or Remove References on MSDN for more details.
Finally, if you absolutely want to manually convert an existing Win32 project, you would need to set the "Common Language Runtime Support" project properties under "General" and the "C/C++ , General" tabs to one of /clr, /clr:pure, /clr:safe or /clr:oldSyntax (depending on your specific application requirements; if you're just toying around you might want to start with /clr) for all Configurations and Platforms as well as specify the targeted .Net framework version by directly editing the .vcxproj (as indicated in this answer). You would also still need to add assembly dependencies as with the new project approach above.
You have to set Common Language Runtime Support (/clr) in Configuration Properties - General:
And in Configuration Properties - C/C++ - General:
You are having a project of Win32 Console Application and and you are using .NET namespace. In Win32 Console Application only 4-5 namespace are there in standard library including std. Try using namespace std; at the top.
I have a C++ project running cinder. I would like to implement a plugin system using LoadLibrary and a .dll file using a custom interface. I got a simple example working, but once I add the cinder libraries to use as a reference in my dll, the dll won't compile, giving an error the and are not compatible with compiling with /clr.
I am a greenhorn when it comes to C++, most of my programming is done in C#. Is there anything I could do about this?
P.S. Switching off /clr gives me a bunch of errors about MT_static and MT_Dynamic mismatches in cinder.lib. I'm going nuts here.
Thank you for your help,
Matija
Well, the solution was not all that forward, but after a LOT of trial and error, this finally worked:
I turned off all clr options (set them to no support in visual studio). The the setting for Dynamic Debug DLL needed to be changed to Dynamic no debug (no DLL). After that, it was just a matter of commenting out Stdafx.h and setting and current setting incompatible flags to compatible.
The project built into a normal .dll and is now working fine :D
I have a lot of unmanaged C++ code written quite some time ago. Now, I'm wanting to go back and add automated tests to that code using MS Test (built into Visual Studio). However, in order to do that, I either have to build the C++ as a .lib file and write a wrapper layer around it (in order to call it from C# tests) or I need to be able to compile the code as managed. The first option I've got worked out, but ideally I'd prefer to use the second option so that don't need the wrapper layer. Note, even though I mentioned writing the Test code in C#, that's not necessarily a requirement.
So, I changed the Project's properties such that the "Common Language Runtime Support" property changed from "No Common Language Support" to "Common Language Runtime Support (/clr)". Now when I try to compile, I get an error that reads
"error D8016: '/ZI' and '/clr' command-line options are incompatible"
Does anyone have idea what this really means and how I can work out the aforementioned problem? Have you done this before? I've been searching and reading about it online now for quite some time and I've really come away empty handed with no real answers.
Any tips or advice would be much appreciated.
Thanks.
The /ZI option is for edit-and-continue with native code.
Try the very similar /Zi option instead. Debug database without the edit-and-continue feature.
I have a written a simple console application in C++ with a couple of .cpp files and one .h file. I have set the assembly to be a dll. When I create a winForms project I cannot add a reference to the dll as it's no managed code. Fair enough. So I go back to my console application and in the project's properties set Common Lanauge RunTime support to /clr.
When I try to compile I get the error:
Error 1 error D8016: '/ZI' and '/clr' command-line options are incompatible
Any suggestions as to how I get my console application into managed c++ for use with a winForms project?
Thanks
Thomas
/clr isn't compatible with /ZI
Check this page out for restrictions when using /clr: http://msdn.microsoft.com/en-us/library/ffkc918h.aspx
I'm a total noob when it comes to linking and building with Visual Studio. I would like to integrate Lua into my C++ console application.
Can someone give a step by step on how to do this from getting the Lua dependencies from lua.org, to actually running a "Hello World from Lua" in VS, and all the steps in between.
Finding something like this online has been very difficult since most require prerequisite knowledge of building Lua and such.
Thanks :)
Start with the Lua for Windows package. It will get you a self-contained batteries included Lua installation. Lua for Windows is not an official distribution, but it is well respected by the Lua user community. You can use its lua.exe to gain experience with the language in a Windows environment, and its rich collection of tested extension modules is also available for use.
If you add its include and lib folders to your VS project configuration, you should be able to compile and link against Lua in short order.
One possible complication is that the LfW distribution is built against VC8's C runtime library. If that becomes a problem, then you can either compile Lua yourself as part of your Solution, or get a known good DLL that matches your specific version of Visual Studio from the Lua Binaries project.
Do remember that if you are using one of the distributed DLLs, it will have been compiled as C, and not C++. That means that you must wrap any references to the Lua include files in extern "C" {...} or you will have problems with linkage.
It really helps to have some experience with VS project configuration and building. In particular, experience with mixing C and C++ in a VS project is very helpful.
I heartily recommend following the advice already given about learning C and C++ and mixing the two together. Once you have that under your belt, you may want to check out LuaBind or LuaPlus for connecting C++ and Lua. You can do it manually (and you probably should, at first, to understand what's going on under the hood), but it's more efficient and cleaner, code-wise, to use one of those binding libraries. For debugging purposes, Decoda is a good choice; it can attach to processes started in VS which contain Lua code you want to check.