Compiling Unmanaged C++ Code as Managed Code - c++

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.

Related

Xcode 11 completion behaviour for project in C++

I'm struggling to change the behaviour of auto-completion for my own project in Xcode11.
I selected an empty project and I have implement some C++ methods. Now I want Xcode to recognize these methods and show the auto-complete suggestions for them.
However, I am only getting completion suggestions for basic C++ code, e.g., if-else, switch case, etc.. How can I setup Xcode to recognize my own methods?
Besides, in an older Xcode version I got auto-complete suggestions for previously written text in the same file. This functionality is gone. Can I reactivate it somehow?
Cheers
Your project should index and make those methods available in auto-completions. I've had similar issues (currently Xcode 11.3.1), but I found that building the project once (even with just a boilerplate main function), quitting and restarting Xcode seemed to help, at least for the project-local classes & methods. However auto-complete is broken now for some parts of C++, as if the standard system search paths for C++ aren't getting indexed. For example, completions for standard C++ header #includes (e.g. ) don't appear, only various Apple C lib headers. It may be that manually adding these paths to the build settings could resolve them. It used to "just work" though.
I'd also recommend filing a bug report if you haven't already. I'm sure they're focused mostly on Swift these days but if enough reports come in they may fix it.

How can I parse C++ code in a prebuild event?

I have a prebuild-event tool (written in Ruby) in my C++ toolchain, that generates additional C++ code from existing C++ source code. I would like to replace this tool with a faster generator and using clang would be the best option.
Is there a way to write a C++ application that parses C++ source code of a file, so I can implement this prebuild tool in Clang? I am looking for a keyword or page with how to start. Any help is highly appreciated!
Parsing C++ is not a simple thing. Compile-time trickery and implicit semantics make it incredibly hard. Because of this I would suggest going with Clang. Its developers made it possible to use Clang as a library. Check out this guide to see different interfaces Clang has. If you want real C++ experience you might want to choose LibTooling.
I want to warn you though that in order for any C/C++ parser to work as expected they absolutely need compilation options used by the real compiler. Without include directories or macro definitions the code can make little to no sense. Basically your build system should tell your custom tool how to compile each file. The simplest approach would be using compilation database. It is a go-to solution for many Clang-based tools. However, it looks like you're making it a part of your build system, so maybe incorporating your tool and using options directly from the build system can be not such of a burden for you.
I hope this information is helpful!

Managed c++ code for C#

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".

How to convert a cmake project into a Visual Studio equivalent?

The situation is the following: I have the source code of one programm (lets call it programA) (written in C and C++), as well as the CMakeLists.txt and CTestConfig.cmake files. I already installed programA using CMake's graphical user interface and, as it is obvious, it worked. It created the .exe file (I'm working on Windows 7 OS).
The problem is that, right now, I've been asked to edit the program (and so, I must be able to edit the code and degugging it as changes are made). I also need to compile it but not in .exe anymore but in .dll so I can add it to a website we have.
I've read in forums that CMake can compile programA into a .dll if I need to, but as I would need to make some changes I consider that CMake debugging is not as useful and easy as using entirely VS. From the little I know from CMake language, the CMakeLists.txt is mainly used to check the OS of the user as well as adding some libraries in case they are not found.
I have to admit I have no idea in programming CMake directives, as I have been working with ASP.NET, C, C++ and C# mostly. Then, my idea is to try to work only in visual studio 2010 instead of using cmake as well, so once I have the program 'adapted' to VS and can be compiled just using VS, I'm ready to start my job. So the question I have is how can I perform the same task CMake did just using Visual Studio (Is there any way of implementing CMake directives in VS?), can VS compile by receiving as an argument something similar to that CMake.txt file (though it needs to be translated into another language)?
To skip the use of CMake I tried to copy the source code into a new project in VS. However as it does not use the CMake directives when compiling, it gives several errors, most of them related to the fact that some headers.h can't be found (cause they might be in a subfolder). And there are so many subfolders to add the paths to the predefined directories of search that it would take ages.
I'm sorry I can't be more precise in my explanation. I'm good at programming little projects on my own, but it's the first time I have to work on other's programm. Please don't hesitate to ask if anything was not properly understood
I would appreciate a lot any suggestion / advice /guidance you can give.
To make a dll, use add_library command and the SHARED keyword
add_library(mylib SHARED ${files})
this is easy with CMake, don't go back in visual that will be harder at the end
The Good News
Fortunately, cmake can generate VS Projects automaticaly for you (this tutorial s specific for OpenTissue, but Steps 1 to 3 should be the same for you).
The [not so] Bad News
Depending on the complexity of the project, VS Projects automaticaly generated by cmake can get pretty nasty, to the point of illegibility. It will, for example, hard link any library dependencies using the specific paths of your machine, so the project will most certainly not be portable across setups. In any case, that's the intended bahavior, because the primary idea of supporting this generator is simply making it work, thus allowing users to easily compile projects using MSVC, so there's not much you can do here. Nonetheless, it should work in your machine and will certainly be a great starting point for you, just create a project yourself from scratch copying the relevant parts out of the automatic generated version.

Dead code identification (C++)

I have a large legacy C++ project compiled under Visual Studio 2008. I know there is a reasonably amount of 'dead' code that is not accessed anywhere -- methods that are not called, whole classes that are not used.
I'm looking for a tool that will identify this by static analysis.
This question: Dead code detection in legacy C/C++ project suggests using code coverage tools. This isn't an option as the test coverage just isn't high enough.
It also mentions a -Wunreachable-code. option to gcc. I'd like something similar for Visual Studio. We already use the linker's /OPT:REF option to remove redundant code, but this doesn't report the dead code at a useful level (when used with /VERBOSE there are over 100,000 lines, including a lot from libraries).
Are there any better options that work well with a Visual Studio project?
I know that Gimpel's Lint products (PC-Lint and Flexelint) will identify unreachable code and unused / unreferenced modules.
They both fall in the category of static analysis tools.
I have no affiliation w/ Gimpel, just a satisfied long-term customer.
You'll want something along the lines of QA-C++ (http://www.programmingresearch.com/QACPP_MAIN.html), also see http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis for similar products.
You're looking for a static code analysis tool that detects unreachable code; many coding guidelines (such as MISRA-C++, if I'm not mistaken) require that no unreachable code exists. An analysis tool geared specifically to enforce such a guideline would be your best bet.
And you'll like be able to find other uses for the tool as well.
I dont know Visual C, and had also recommended the -Wunreachable-code specific coverage tools. As solution for your situation I would try the following:
Make with ctags (or similar programm) a list of all your symbols in your source
Enable in your compiler the dead code elimination (I would assume it defaults to on)
Enable your whole-program/link time optimizations (so he knows that not used functions in your moduls are not required by other externals and get discarded)
Take the symbols from your binary and compare them with the symbols from 1.
Another approach could be some call graph generating tool (e.g. doxygen).
I suggest you use a couple approaches:
1. GCC has some useful compilation flags:
-Wunused-function
-Wunused-label
-Wunused-value
-Wunused-variable
-Wunused-parameter
-Wunused-but-set-parameter
2. Cppcheck has some useful features like:
--enable=unusedFunction
3. Use static analyzer as was suggest before.
One approach that works for me - with Delphi - is to enable debugging, and run your program under the debugger.
When a Delphi program is run under the debugger, the IDE shows in the margin which lines of code can be set as breakpoints. Code which is truly dead - i.e., has been stripped out by the linker/compiler is obvious as breakpoints can't be set there.
Some additional notes, as commenters seem to misunderstand this:
a: You don't need to try setting a breakpoint on each line. Just open up the source file in the IDE, and quickly scroll through it. Dead code is easily spotted.
b: This is NOT a 'code coverage' check. You don't need to run the application to see if it reaches the lines.
c: I'm not familiar enough VS2008 so can't say if this suggestion will work.
Either
1) MSVC's under-used in built static analysis tool.
2) The MSVC marketplace has lots of tools including support for most free tools, including CppCheck
You will need the latest version of Visual Studio for market place applications, but the free "Community Edition" has very lenient licencing.
Write a script that randomly deletes a function (from the source code) and recompiles everything from scratch. If it still compiles - that function was dead code.