Cpp-CLI-FAQ

De Ensiwiki
Aller à : navigation, rechercher

Sommaire

Questions about Visual Studio

Where can I find the Visual Studio command line?

The Visual Studio command line is equivalent to 'cmd.exe', but contains additional definitions for environment variables that are specific to Visual Studio.

Visual Studio tools ->  Visual Studio 201x command line

Where can I specify additional include directories?

Project properties -> C/C++ -> General -> Additional Include Directories

Where can I specify additional library directories?

Project properties -> Linker -> General -> Additional Library Directories

Where can I specify the libraries that the project will depend upon?

Project properties -> Linker -> Input -> Additional Dependencies

How can I set the solution to be compiled in 64 bits?

Go to

Solution properties -> Configuration

Click on the 'Configuration Manager...' button, select 'New...' for the active solution platform and choose 'x64'.
Configx64.png

How can I make IIS Express run as a 64 bit process?

Go to

Tools -> Options -> Projects and Solutions -> Web Projects -> Use the 64 bit version of IIS Express

How can I avoid using precompiled headers?

Project properties -> C/C++ -> Precompiled headers

How can I compile C code as if it were C++ code?

Project properties -> C/C++ -> Advanced -> Compile as

How can I debug managed and native code at the same time?

For Windows Forms and WPF applications?

  1. Go to Tools -> Options -> Debugging, and select Use Managed Compatibility Mode (bottom of the list on VS2013)
  2. Reach the Managed project properties -> Debug and select Enable native code debugging

For ASP.NET applications?

Things are a bit more involved for web applications. In general it is much more efficient to have a minimal interface (Windows Form, Console Application...) to work on debugging the native code and the wrapper.

  1. The native pdb fils need to be found by the server. The simplest way for that to happen is to copy them along with the native dll and lib files.
  2. In the Properties file of the Web project, select Native code in Web tab.
    Select Native code
  3. Go to Tools -> Options -> Debugging, and select Use Managed Compatibility Mode (bottom of the list on VS2013)
  4. Unfold the Debugging tab of the previous step and select Microsoft Symbol Servers
    Select Native code
  5. Run the debugger. The first time you try to enter a native function, you will normally obtain a display like the one below:
    Source not available
    This can happen because the native dlls were added to the Delay Loaded Dlls field of the C++/CLI project. After continuing in the debug session, the corresponding native dll should have been loaded:
    Dll loaded
    The next time you try to enter a native function, everything should work correctly.

How can I add pre-build and post-build events?

Project properties -> Build Events

How can I automatically copy native dlls into the right managed directories?

This should be done in the post-build events of the native project. That way, each time the native project is modified and rebuilt, the new version of the dll will be copied to the right directory.
For example, if a native dll called TestASP.dll has to be copied to the bin folder of an ASP.NET project called WebSite, the command line is:

copy "$(SolutionDir)Debug\TestASP.dll" "$(SolutionDir)WebSite\bin\TestASP.dll"

How can I add a main to my dll to test what I have coded without going through a wrapper and a C# interface?

After adding a main() to your code, go to

Project Properties -> Configuration Properties -> General

In the Project defaults, select the Application configuration type.
Then go to

Project Properties -> Configuration Properties -> Linker -> System 

Select the Console subsystem.

How can I change my C++ project type (dll or Windows Application) to Console Application?

Go to

Project Properties -> Configuration Properties -> General

In the Project defaults, select the Application configuration type.
Then go to

Project Properties -> Configuration Properties -> Linker -> System 

Select the Console subsystem.

Questions on libpnl

Where can I find libpnl?

At this address

How can I install libpnl under Windows?

In order to use libpnl, you only have to unzip the downloaded libpnl file.

How do I properly link libpnl in a Visual Studio project?

  1. Add libpnl.lib to the project dependencies (as described here)
  2. Add the directory containing libpnl.lib to the additional library directories (as described here)
  3. Add the directory containing the header files to the additional Include directories (as described here).

I cannot compile because of macroizing errors in xkeycheck.h

A basic solution is to make sure all the .hpp files you include are included after the system header files. Another cleaner solution is to define _ALLOW_KEYWORD_MACROS in the project settings.

How can I specify that the necessary dlls should be copied into the folder containing the built executable file?

After you have specified a name for the executable file (e.g. with the add_executable(<executable_name>) instruction), add the instruction

pnl_add_postbuild(<executable_name>)

More general problems

The 'C/C++' tab does not appear in the properties of the Visual Studio project.

The project must contain at least one '.cpp' file before the tab appears.

No '.lib' file is generated after I build the native project.

It is necessary to add the '__declspec( dllexport )' directive in the header file, and implement at least one exported function.

I cannot add any project to the solution because the 'Solution' tab is missing

Go to File -> New -> New project, and choose Add to the solution in the Solution menu.