dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 45

Thread: [RESOLVED] Re: How to learn c#

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    [RESOLVED] Re: How to learn c#

    Quote Originally Posted by Arjay View Post
    rather than going C++/CLI, I would put my C/C++ code inside a dll and call the dll inside a C# .Net assembly using pinvoke. That way, you can have the code written in C/C++ utilized and get the benefits of coding in a .Net tailored language like C#
    Many thanks Arjay (and 2kaud) for all your help with this. I tried my first tentative steps with C# this morning by building the ConsoleApp sample which comes with VS2019 (it just prints out Hello World if run from a Command Prompt).

    But of course, the generated app is an exe file so it'll only run in Windows. Suppose I wanted to build it so it'd run under OS-X. Can that be done from within my Windows version of VS (i.e. by cross-compiling)? Or can it only be done by building with the Mac version of Visual Studio in OS-X?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: How to learn c#

    I must admit that I've never done it, so I had to look it up. I found the following: https://stackoverflow.com/questions/...tion-on-ubuntu

    It's actually pretty simple to build from your Windows machine in Visual Studio. Now, I tried this out on VS 2017 (and VS 2019 might be even easier), but you make a small edit to the project file and then publish the project.

    1) Edit the project file (highlight the .net core console app project in the solution explorer, right click and choose "Edit xxxx.csproj".
    2) Underneath the <targetframework/> node, add
    <RuntimeIdentifiers>win10-x64;osx.10.11-x64;ubuntu.16.10-x64</RuntimeIdentifiers>
    3) Save and close the file.
    4) Build the project

    Note: The first time you build, it will take longer because it will download the necessary nuget packages needed for the cross-platform build.

    After you build, you can publish for Linux.

    To publish:
    1) Click on Build\Publish...
    2) In the Pick a publish target dialog, in the left pane, click on the Folder option (don't choose Microsoft Azure App Service)
    3) Click "Create Profile"
    4) Click on "Configure..."
    5) Change the Deployment Mode to "Self-Contained" (this builds for the target without requiring .net core to be installed).
    6) Change the Target Runtime to "linux-x64"
    Note the Target Location because this will be where the built binaries are located.
    7) Click Save
    8) Click Publish

    After the build completes, copy the contents of the publish folder onto the Linux machine (note: you'll see a file without an extension and with the same name as the project. This is the executable to run in Linux).

    As I mentioned, this is for Visual Studio 2017. I'll double check it's the same for Visual Studio 2019 when I get to work tomorrow.

    Lastly, you can create a "Framework Dependent" deployment mode, but you'll need to install .Net Core onto Linux (which would make sense if you are planning on running other .net core apps on Linux).
    Last edited by Arjay; October 17th, 2019 at 01:15 AM.

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: How to learn c#

    That's great detective work Arjay, many thanks.

    The problem now is when I press Build->Publish. Here's the dialog I see (and even after pressing Next or Finish I never see anything saying "Create Profile")

    Name:  Capture.jpg
Views: 58
Size:  31.1 KB

    [Edit...] I found a different window under Properties->Publish. Just wondering if this one's more like the one you're seeing?
    Attached Images Attached Images  
    Last edited by John E; October 17th, 2019 at 06:01 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: How to learn c#

    John, you created the wrong project type. You created a "Console App (.Net Framework)" project. Instead you should create a "Console App (.Net Core)" project.

    Btw, on 2019 there is a slight variation to my instructions.
    1) you don't need to edit the project file (i.e. no need to add the <runtimeidentifiers> node)
    2) Create the profile the same way, but click "edit" to change the settings (rather than clicking configure...)

    When you get this working, it will make you smile.

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: How to learn c#

    Many thanks again Arjay.

    Earlier today I repeated the same experiment but using VS2015 on a different machine. This time I'm pretty sure I did select the .Net Core project and yet it gave me exactly the same result. It's getting late here now but tomorrow I'll wipe those projects and try again.

    In the meantime... is there any way I can tell if a project's configured to use .Net Core? For example, is there something in the .csproj file that would identify it as .Net Core?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: How to learn c#

    Try to avoid using 2015. Use at least 2017 but preferably 2019. The reason is 2019 will have better cross platform support and the documentation will be up to date. For older versions, it will be harder to wade through the workarounds needed at that time (but are no longer required).

    You can check the target framework by right clicking on the project, choose properties, and look at the 'Target framework:' field under the Application tab.

    Examples:
    .NET Core 3.0
    .NET Framework 4.6.1
    Last edited by Arjay; October 17th, 2019 at 01:40 PM.

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: How to learn c#

    Thanks again Arjay (I do appreciate your persistence with this !!)

    Things have now gotten even weirder... I launched VS2019 this morning but it wouldn't let me do anything until I updated (which I did). I then checked the target framework like you described and it says .NET Core 3.0. But after the update, I tried Build->Publish and it now doesn't show me any dialog at all for a .NET Core 3.0 project (or any .NET Core project). But if I load any .NET Framework project, I see the same dialog I saw yesterday.

    I started wondering if maybe .NET Core 3.0 wasn't installed somehow so I ran the installer and looked at Individual Components. .NET Core versions 3.0, 2.2 and 2.1 seem to be installed but the earlier versions are described as X.X Runtime whereas 3.0 is described as 3.0 SDK. Dunno if that might be significant?

    [Edit...] FWIW here's what the installer shows me:-

    Name:  Capture3.jpg
Views: 53
Size:  33.5 KB
    Last edited by John E; October 18th, 2019 at 03:26 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,906

    Re: How to learn c#

    Or can it only be done by building with the Mac version of Visual Studio in OS-X?
    See http://www.vbforums.com/showthread.p...o-Run-on-a-Mac
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

  9. #9
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: How to learn c#

    Thanks 2kaud. That thread seems to indicate that cross-compiling from Windows isn't possible and yet we know it is (Arjay achieved it a couple of days ago with VS2017). What I can't do is make it work with either 2015 or 2019. I must admit, I'm considering uninstalling 2019 now and giving 2017 a try.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,906

    Re: How to learn c#

    That indicated that x-compile for Mac isn't possible. I understood Arjay posts referred to Linux?

    Code:
    To publish:
    1) Click on Build\Publish...
    2) In the Pick a publish target dialog, in the left pane, click on the Folder option (don't choose Microsoft Azure App Service)
    3) Click "Create Profile"
    4) Click on "Configure..."
    5) Change the Deployment Mode to "Self-Contained" (this builds for the target without requiring .net core to be installed).
    6) Change the Target Runtime to "linux-x64"
    But you'd probably be able to publish for anything you can set the Target Runtime to in 6) above?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

  11. #11
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: How to learn c#

    Quote Originally Posted by 2kaud View Post
    That indicated that x-compile for Mac isn't possible. I understood Arjay posts referred to Linux
    Well I guess that's something else I should clarify with Arjay. Here's the output I see when building the MS Console app. Note that it only refers to a Windows DLL (though in reality, it also creates an exe). It'd be interesting to know what Arjay's seeing in his build output...

    Name:  Capture4.jpg
Views: 51
Size:  10.4 KB
    "A problem well stated is a problem half solved. - Charles F. Kettering

  12. #12
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: How to learn c#

    There's been some major progress with this !

    Late yesterday I decided to uninstall VS2019 but when I ran the installer it told me that version 16.3.5 was available. The update to 16.3.4 had only happened a few hours earlier but I figured there'd be no harm in trying the newer version - and believe it or not, it now works!!!

    When I say "works" I mean that Build->Publish is finally showing me the correct dialog window. I'm not sure yet what it's actually generating & publishing for the non-Windows platforms but I don't think it's an actual runnable version of the app. The only runnable things I can see are the Windows exe file and a few .json scripts (which I don't think contain the actual app). I'll investigate a bit more....


    [Edit...]

    Hmmm... it looks like I'm wrong about that. Doing a Build or Rebuild only seems to build the Windows target - but a Publish does seem to build something runnable - at least I'm guessing it's runnable because when I tried running it under OS-X it gave me this message:-

    A fatal error occurred. The required library libhostfxr.dylib could not be found
    Hopefully, if I install .NET Core in OS-X that'll fix it
    Last edited by John E; October 19th, 2019 at 03:15 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: How to learn c#

    Yes, the process is to Publish for the non-Windows platforms.

    To avoid having to install .NET Core on the target, in the publish settings change the 'Deployment Mode' to 'Self-Contained'.

    If you've already set this to 'Self-Contained', try looking at: https://github.com/dotnet/core-setup/issues/3400 .

    Btw, I'm in the process of installing 2019 Community edition.

    [Edit] I just finished the 2019 Community edition install. The only workload I selected in the install was .NET Desktop Development.

    Then created a new Console App (.Net Core) app. Set the publish settings to 'Self-Contained' and 'osx-x64' and published.

    Files were created in the publish folder including libhostfxr.dylib.

    This is working for me on VS 2017 Enterprise, VS 2019 Professional, and VS 2019 Community.

    If this isn't working for you, we'll need to review the install workload and the publish settings.

    Name:  VSOsxPublish.jpg
Views: 50
Size:  36.6 KB
    Last edited by Arjay; October 19th, 2019 at 12:09 PM.

  14. #14
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: How to learn c#

    Quote Originally Posted by Arjay View Post
    Btw, I'm in the process of installing 2019 Community edition.
    I must admit, I've found it to be pretty flaky

    Although the Publish feature now works for me with a .NET Core app, it seems to have broken now for .NET Framework. Previously, publishing a .NET framework app (for Windows) would generate a setup.exe file but since updating yesterday to 16.3.5 it now gives this error:-

    Error - Default certificate could not be created. Publish aborting. Build has been canceled
    It seems like I'm allowed to have one working or the other - but not both...
    "A problem well stated is a problem half solved. - Charles F. Kettering

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: How to learn c#

    Quote Originally Posted by John E View Post
    I must admit, I've found it to be pretty flaky

    Although the Publish feature now works for me with a .NET Core app, it seems to have broken now for .NET Framework. Previously, publishing a .NET framework app (for Windows) would generate a setup.exe file but since updating yesterday to 16.3.5 it now gives this error:-



    It seems like I'm allowed to have one working or the other - but not both...
    There seems to be some confusion. A .NET Framework app is not cross platform and only runs on Windows (yeah, I'm ignoring any older .NET Framework ports that may allow this).

    At at any rate, all my posts are directed at .NET Core. Does publishing a .NET Core console app as self-contained with a target of osx-x64 generat files that run on osx? If so, you are done for .NET Core.

    If you want to build and publish a .NET Framework app, you'll need to start with a new 'Console app (.NET Framework)' project. You can publish but I'm not sure what the publishing options are but there isn't any reason you couldn't publish the two different project types (but cross platform requires starting with .NET Core).

    You might want to focus on .NET Core because it's the replacement for .NET Framework, so the benefits of learning .NET Framework may be short lived.

    As far as the newer VS install, it's different than the older installer. I know it was around for 2017, but maybe it was there for 2015 as well (or one of its sp updates). It works very well, but different if you haven't seen it before. The nice thing about it is that you can change workloads or individual components by running the Visual Studio 2019 Installer from the start menu at any time.
    Last edited by Arjay; October 19th, 2019 at 03:22 PM.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)