Why do native C++ projects have a TargetFrameworkVersion? - c++

In Visual Studio (v100, v110) it is possible to specify the version of .NET framework that a project targets using the TargetFrameworkVersion element in the project file. (If there is no TargetFrameworkVersion element, the IDE just uses its default .NET version.) The specified TargetFrameworkVersion is then used to choose the (default) tool-chain which will be used to build the project.
The above is true for both CLR and native C++ projects. I find this really weird and confusing. If Visual Studio knows a project is native then why does it care what the TargetFrameworkVersion is?

Well, actually you'd have to ask the developers responsible for creating the MSBuild scripts, because in principle it is not really needed, nor used. And they know it themselves. For a standard C++ project file, these are the lines causing the property to get set (Microsoft.Common.targets):
<!-- By default, we are creating a managed app because .NET 2.0 projects did not have this property. -->
<PropertyGroup Condition="'$(TargetRuntime)' == ''">
<TargetRuntime>Managed</TargetRuntime>
</PropertyGroup>
<!-- Because .NET 2.0 apps did not set TargetFrameworkIdentifier, we need to set it for them here by default. If
the runtime is set to Managed, we also need to set these. Otherwise they should be blank (for instance Javascript or
Native apps) because they do not target a .NET Framework. -->
<PropertyGroup Condition="'$(TargetRuntime)' == 'Managed'">
<TargetFrameworkIdentifier Condition="'$(TargetFrameworkIdentifier)' == ''">.NETFramework</TargetFrameworkIdentifier>
<TargetFrameworkVersion Condition=" '$(TargetFrameworkVersion)' == '' ">v4.0</TargetFrameworkVersion>
</PropertyGroup>

Lazy programming, basically. Microsoft historically preferred managed code because it's not really portable to other operating systems, causing lock-in. So the .NET languages have had more priority in Visual Studio.

Related

I'm trying to use native C++ libraries in a MAUI app and cannot get it working for MacOS/iOS

There's a great article here that describes how to use native C++ libraries in Xamarin/C#, and I would assume that this would extend to MAUI: https://learn.microsoft.com/en-us/xamarin/cross-platform/cpp/
I followed these instructions, and put together an end-to-end demo solution of it here: https://github.com/whodges/bindingsample. This does require that you link Visual Studio to a MacBook to build the full solution.
That example essentially uses the default MAUI template, and modifies it slightly so that the actual 'click counter' value is set and retrieved from a native library on a per-platform basis. For Windows, that's a C++ DLL (works great). For Android, that's a .so library (works great). And for iOS, that's a static .a library, and here's where I'm running into trouble: when I try to launch this app using the iOS Simulator, it fails with a clang++ error, and something along the lines of "Could not extract the native library 'libCounteriOS.a' from .../obj/Debug/net6.0-ios/iossimulator-x64/linker-cache/libCounteriOS.a'. Please ensure the native library was properly embedded in the managed assembly (if the assembly was built using a binding project, the native library must be included in the project, and its Build Action must be 'ObjcBindingNativeLibrary')".
I created a C# .NET iOS Binding Library project (Counter.iOS in my example), and this is supposed to bundle libCounteriOS.a, which is generated when CounteriOS (a C++ project) is compiled. First, when Counter.iOS is built using Visual Studio on Windows, it fails to generate the Counter.resources folder in its 'bin' folder, along with the subsequent 'manifest' file. When I use Visual Studio for Mac, these files do get generated. So, I stuck with Visual Studio for Mac to build the iOS.Counter project (everything else is built with Visual Studio on Windows).
Regardless, I still get the "could not extract the native library" error I described. I've tried setting the library file's Build Action in Counter.iOS to ObjcBindingNativeLibrary, but that just results in an unhelpful "Build failed - see build log for details" error in Visual Studio for Mac, and I cannot find said log file for the life of me. On top of that, it's not an Objective C library anyway - it's a C++ one - so I'm not sure if this step is actually appropriate. Does it have to be Objective C? The Xamarin article suggests otherwise, and the native reference option 'Is C++' is there as well. I've also tried setting libCounteriOS.a's Build Action to 'BundleResource' - no luck with that either. I've tried it with .NET 7.0 as well - also no luck there.
I'm really at a loss. Is this even possible? It doesn't seem to work and I've been poking at it for weeks on it. That Xamarin article seems to suggest it should be possible. Any help would be greatly appreciated.

Microsoft Visual cannot add tools

I´m new at Visual Studio C++ and maybe I´m asking a very trivial question. I have a project/application but I have to add a few new features to it. When I open the project in MVS and in "dialog" folder there are windows (or dialogs?) used by application and I can modify them but I´m not allowed to add components/tools that I really need. I have only Dialog editor tools unlocked. I can compile an run application but When I try to add a form to project I´ll see a message:
You are adding a CLR component to a native project. Your project will be converted to have Common language runtime support.
I´ve googled some information about .NET forms and windows dialogs, but I do not know what to do next. If I choose "yes" (convert project) I cannot compile it anymore.
What can I do if I would like to use a ZedGraph controll to plot graphs from data in this app?
The C# GUI tools are different to the C++ tools (like MFC).
When you try to add C# tools to a C++ project the IDE warns you "You are adding a CLR component..."
Depending on which IDE you are using, when you bring up the resource view (http://msdn.microsoft.com/en-us/library/d4cfawwc.aspx) e.g. with ctrl + shift + E you should be able to find the existing dialogs and double click to edit them.
It seems that ZedGraph is a C# library (from the docs), so you will have to convert into a C# project in order to use it, which will not be straightforward, or use a suitable C++ one.

Cross-machine DLL creation using VC++

I've created a DLL file using VC++ 2008 with following settings:
Configuration type: Dynamic Library (.dll)
Use of MFC: Use Standard Windows Libraries
Use of ATL: Not using ATL
Common Language Runtime support: Common Language Runtime support (/clr)
The created DLL is working perfectly on my machine (the machine it's created on) but it can't be used on another machine (I tested with 3 machines). For more details, I load this DLL file via JNI (Java Native Interface). On my machine it works, but on others it showing the error of "java.lang.UnsatisfiedLinkError:: The application failed to start because its side-by-side configuration is incorrect...."
My question is:
1. Did I miss any configuration when creating the DLL that can't be worked on multiple environment?
2. Can we created a DLL file that will work on different machines?
Thank you so much!
You need to make sure that the VC++ 2008 Redistributables have been installed on the other machines. To double check this is the problem run Dependency Walker on the other machine and it'll show you what it's looking for:
http://www.dependencywalker.com/
You can find the VC 2008 redist EXE on the microsoft web site:
http://www.microsoft.com/en-us/download/details.aspx?id=29
or the actual DLLs are in the redist folder in the VC 2008 install folder.
Actually you can google for "side-by-side configuration is incorrect" and you will find a lot of hints what the reason could be.
But ... giv this a try first:
In VS, project settings, C/C++, Code Generation: Set "Runtime Library" to "Multi-threaded" (or "Multi-threaded Debug".
(Btw: Do you need Common Language Runtime support?)
Besides what #snowdude says, which is correct, there is another possible problem: if these other PCs don't have VS2008 installed, and you linked to the debug MSVCR* libraries, you're out of luck. Only Release mode binaries can be deployed to user systems without hoop-jumping.

how to create installer file

I am currectly finishing my project in C++ and i'm looking for a way to create my own C++ installer file
which will create the project dll's and exe files into a specific path
what is the easier way to learn how to do it?
There are several ways to build an installer. While you can of course always make one yourself, you should google for something like "create an installer". Some prebuilt solutions include "InstallShield", or the ".msi" file format, which you can create on your own using something like "Advanced Installer".
Of course, if you want your users to build your project from source, then you need a makefile and to make sure you bundle all the libraries. There are also kits like autotools to do that for you.
If you use VS 2010, Installshield LE would suffice as it is integrated into VS 2010.
If you have access to Installshield IDE, there is nothing better available for your packaging needs.
There are two ways of packaging:
a) The LEGACY way
b) The Windows Installer way, Basic MSI is the keyword here.
The LEGACY way involves creating your own scripts for:
a) Installing the files to their locations
b) Writing registry entries, if needed
c) Registering COM components, if needed
d) Creating shortcuts etc...
Tools that can be used for LEGACY approach are:
a) NSIS - very good and has a scripting language of its own.
b) Installshield - has a project type called Installscript Project. Installscript is the scripting language to be used.
The Windows Installer way is a bit hard comapred to the LEGACY way.
One has to learn the basics of MSI technology which can be daunting.
The package created has .msi as extension. This file is a database that the developer configures and the Windows Installer takes care of all other things. This is called TRANSACTIONAL installation procedure.
Even the UI presented during install is configured in the Database using tables like Dialog, Controls etc...
Tools that can be used for Windows Installer approach are:
a) Installshield - has a project type called Basic MSI
b) Wix - Opensource and xml based. You configure appropriately named xml files and various utilities in the Wix package will help you to create an MSI package.
First after completing your project click save. Second click on file tab,Add,New Project.
In new Project Click other Project types , Setup and Deployment and in that You can click InstallShield LE or Visual Studio Installer.
Hope this Helped you
I always recommend NSIS. You might also investigate HM Nis Edit - it's an IDE for NSIS that has a useful wizard feature. It will generate an installer script for you which you can further customize. The documentation is extensive.
can't believe no one mentioned install creator pro(there is also a free version with a branded message that displays after installing). Its feature set is pretty limited, though it has options for writing registry values and specifying custom paths to %AppData% or any other place you may want to install some files. it also has an optional wizard interface, and with each step you have the oppurtunity to preview each individual page.
the paid version offers the option for adding serial number/registration to your program. i've never tried it so i'm not sure how effective it may be. its also quite and expensive little program, but i would recommend it atleast to the beginner or someone who is more concerned with maintaining the codebase of their program and less concerned with how fancy and decorated their installer program is.
i know its been a long time since this was posted but for future reference this program is far easier for any beginner to creating installation packages for the first time

Android C++ Native Code

I am a new Android developer and I would like to create an application using only C/C++ code for Android but I have found the documentation to be very limited. I can create an Android C/C++ project in eclipse, but it uses a lot of java code.
I'm using NativeActivity (new to 2.3) and I need help setting up my project. Does anyone know how to do this?
http://developer.android.com/reference/android/app/NativeActivity.html
Just remove all of the generated Java code. You don't need it if you want a purely native activity. The only thing you need to do is to set up the Android Manifest file as shown in the documentation. In particular, you'll need:
<!-- Tell NativeActivity the name of or .so -->
<meta-data android:name="android.app.lib_name"
android:value="native-activity" />
And you'll need to modify jni/Android.mk so that it builds a jni with the name lib_name.
Yuo can look into the Lighthouse project for android, which allows you to use Qt (and therefore C++) code instead of java. You still need 1 line of java code to kick off your Qt app.