Add dead code warning of C code to Visual Studio 2022 - c

I use Visual Studio 2022 to build my C project and OpenCppCoverage to check the coverage.
With these tools, I don't find how to find C functions (static & public) that aren't called.
The /Zi option may not produce a PDB with the uncalled function and OpenCppCoverage is unable to detect it:
dead Code
As the goal is to detect dead code, one option is to add a warning to visual studio 2022 compilator or linker to raise an error in case of unused function as gcc or to produce a PDB that contain information about the uncalled function (The second option may not work).
Anyone have an idea to detect dead code with these tools?

Related

Using C Functions like printf in Visual Studio 2019

I've recently started write C based DLLs on Microsoft's Visual Studio 2019 (as I am used to traditional C over C++) and some reason I can't seem to get it to compiled with any common C functions like printf(), fopen() etc. I've tried everything from including stdio but it keeps printing unresolved external symbol __stdio_common_vfprintf when I tried using the cstdio instead, it showed a bunch of errors, identifier "using" is undefined, I have never experienced this before as I used to write in C on Xcode. Please help me.
In case it helps my ultimate goal is to make a logger class that logs messages to a console window created by AllocConsole, so I'm trying to do it with stdout and printf().
heres a pic

Cause of "entry point could not be located" in unrelated DLL?

Before you dismiss this question as a repeat, this question is different than the many other similar questions on SO because the function in the error message is completely unrelated to the DLL.
The app and version info
I've got a 32-bit app in Visual Studio 2008 RTM which uses a MATLAB Compiler 4.17 (R2012a) generated DLL, Glib (2.32.4, binaries from GStreamer SDK 2012.9 32-bit), and Qt (4.8.3-vs2008), all running on Windows 7 x64 SP1.
The problem
The debug build runs fine, but in release I get the infamous The procedure entry point libXXXInitialize could not be located in the dynamic link library libglib-2.0-0.dll. The bizarre thing is that this libXXXInitialize function (which does exist in the MATLAB-based DLL) is totally unrelated to libglib-2.0-0.dll.
My research
I checked the obvious, ensuring that all libraries were compiled with VS2008 (except for GStreamer/GLib which is compiled with mingw, but since it's straight C there shouldn't be any issues), and checking for any other versions of the DLLs in the path. It seems like it must be some sort of linker problem.
Update 1
In Dependency Walker x86, it shows this libXXXInitialize as an unresolved (parent) import from not only libglib-2.0-0.dll, but all the other GLib/GStreamer DLLs as well, however the function is correctly resolved in the DLL where it should be found, libXXX.dll.
Update 2
I removed the call to libXXXInitialize and called mclInitializeApplication from the MATLAB Compiler Runtime, and now that function is failing to be resolved from the glib DLL.
However I found a solution (at least temporary), changing the linker options from /OPT:REF to /OPT:NOREF fixed the problem (so the linker "keeps functions and/or data that are never referenced). The bizarre thing is, I certainly use the mclInitializeApplication function explicitly, and if I turn on /VERBOSE:REF, it doesn't show it being removed.
Update 3
Finally found mention of this somewhere else, on the bugtracker for binutils. I found this in a MSVC2010 property sheet as part of the GStreamer SDK. Suggested workaround is what I'm already doing, disabling OptimizeReferences.
If anyone can tell me why this is happening, I'll mark it as the answer.

Windows C Runtime library not linking like I expect?

Being a long-time POSIX/Linux/BSD C developer and now trying to do a few things with Visual C++ 2010, I'm kind of fumbling around with the tooling because it doesn't quite work the same as the GCC toolset does. In theory, at least according to the docs, it seems very similar (and I'm sure it is), but I'm not seeing the things I'm expecting to see.
In this case, I'm trying to build a C library for later use in a .NET project, and I want to be careful to avoid runtime conflicts later on. My thought was to build a static library, linked against a DLL version of the C runtime (msvcr100d.dll in the Debug config) so I set the /MDd flag. However, when I use dumpbin.exe to inspect the resulting .lib output, it shows no dependents or imports.
Two questions, really:
Is this a bad idea in the first place? Should I be building a DLL or linking against the static CRT instead?
Why doesn't dumpbin.exe show the dependency on the CRT when I use /MDd?
Update: Choosing the Correct C/C++ Runtime Library is pretty helpful and informative. I tried the dumpbin incantation he suggests, and the output shows that the lib does link to msvcrtd as expected, but that doesn't really answer my second question above.
Because this is not a final EXE or DLL there is no import section. Instead, there are linker directives that say how this lib would like to be linked.
dumpbin /directives thoraway.lib
Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file throwaway.lib
File Type: LIBRARY
Linker Directives
-----------------
/FAILIFMISMATCH:"_MSC_VER=1600"
/FAILIFMISMATCH:"_ITERATOR_DEBUG_LEVEL=2"
/DEFAULTLIB:"msvcprtd"
/DEFAULTLIB:"MSVCRTD"
/DEFAULTLIB:"OLDNAMES"
/EDITANDCONTINUE
Summary
CFC .debug$S
4C .debug$T
A2 .drectve
B .rdata
4 .rtc$IMZ
4 .rtc$TMZ
49 .text

Linking different C source file in Visual Studio 2010

I am using visual Studio 2010 for compiling a bunch of source File. I am new to it and I wonder how the object files are linked during compilation. Like in Linux we have make file and rules. Does this suppport makefile? If not how can I compile and set flag during compilation.
You can use makefiles and other command-line tools to build using Visual C++ but more typically you handle this by using the GUI to set properties at the Project and File level - start here for info on this.
The Project/File properties adjust the command-line options passed to the compiler and linker for each compilation unit. You can view the actual command line that's used in the GUI too.

How do I simply compile a C program for release with Visual C++ 2010 Express?

I hope the answer to this question isn't too simple or obvious. I've been trying to get used to using a text editor and MinGW to compile some C code but I've found that I can't quite tear myself away from Visual C++ (mostly because of the IDE). The thing is, I've never used Visual C++ to just compile C/C++ into an executable; I always used the debugger and ran the program from inside Visual C++.
I just want to compile my program into a simple executable. How do I need to set up Visual C++ 2010 Express to do that? Don't I just set it to release mode and compile? Is there anything else I need to do (any settings I need to change)? Thanks.
If you've run the program in the debugger, then you've already compiled it!
Just find the executable in the correct folder. If it's a debug build, it'll be under Debug/, similarly for Release/
Note that you can't distribute a debug binary because it depends on the debug CRT which will only be available on a machine with VS installed.
If you just want to run existing code that you have written then you can compile in debug or release mode in Visual Studio. You code will be compiled into an executable file that you can find in the Debug or Release folder in your project. Just double click this file to run it.
Now, if you compiled the debug version, it will be much bigger that the release version and it will (probably) link with debug versions of the C runtime and other VS DLLs. It will still run independently of the debugger though.
It is a common mistake to release or test an application that has been compiled in debug mode. The debug builds often don't expose some bugs as they run more slowly and the executable code is not optimised by the compiler.
If you want to write C appilcations, you can still use Visual studion - just write your code in files that you save as file.c not file.cpp. The compiler will figure it out.
If you want to write C code with a different IDE/compiler, I'd recommend Eclipse as an IDE and GCC as the compiler. MinGW is generally used to build applications that are targeted at Linux, you don't need it if you just want to write in C for a Windows platform.

Resources