How to prevent Visual C++ from creating stdafx.h? - c++

I'm using Visual Studio 2015. I always uncheck "Precompiled header" whenever I create a new project, but Visual Studio still creates the file and adds it to my project. I've tried just creating an empty project, but that also gets rid of the external dependencies, which I need for my projects.
I've tried searching the internet but couldn't find anything about this. Is there a way to prevent Visual Studio from generating stdafx.h but keep the rest of the core libraries at project creation?

Haven't used VS since 2010 but from what I recall you cant get rid of stdafx.h as thats the standard way VS makes a non-empty project for you (it might be just winapp projects, console projects did not have it in VS2010). It wants you to place all your includes into there. Your best bet is to create an empty project and manage the dependencies yourself. Its not too bad.

Just create an Empty Project.
The External Dependencies "folder" in the Solution Explorer will populate itself as you start including external dependencies.
When you create a Win32 Console Application, the External Dependencies will be prepopulated with a handful of headers at first; this is because the Win32 Console Application project includes a few source files that include external headers. When you create an Empty Project, the External Dependencies will be empty at first because there are no external dependencies yet.

Try creating an empty project and place your important libraries manually. I don't think you can start VS non-empty project without a header file.

Related

C++ detours linking issue

I have problems building my code that is using static lib detours. I am trying to do an old basic CTF. For that I want to get into detours.
Whenever I try to build my .dll file I get an issue
LNK2019 unresolved external symbol _DetourTransactionBegin#0 referenced in function _DllMain#12
Now, I have built the detours library using 3 different version of the visual studio dev console.
I have tried firing 'vcvars32.bat' and then using nmake to build the library which was able to build it, but I get the above error during linking my .dll. I have also tried building it with 'vcvarsamd64_x86.bat' and then using nmake to build it which also was able to build the library, but I still get the same error as above during linking.
I have tried the usual stuff: the include folder for detours.h is added to C++/General/Additional Include Directories.
Under Linker/Additional Library Directories I added them as follows: "C:\temp\det_retry\lib.X64";"C:\temp\det_retry\lib.X86";%(AdditionalLibraryDirectories).
And also under Linker/Input/Additional Dependencies I have the following: detours.lib;%(AdditionalDependencies)
What am I missing here? This is a blocker for me for a couple of days and I am reiterating the same steps trying to figure out what's missing but I cannot see. I'd really appreciate the input.
I am sure I am using the newest version because I have downloaded (cloned) detours from the ms github page.
It appears your "Additional Library Directories" are setup incorrectly or contain invalid entries rather. They look like actual library file entries (i.e. pointing to some specific files) versus being only directories (e.g. "my/lib/path/for/my_project/"). Visual Studio's naming conventions are somewhat cryptic but they should be directory entries only. There should be an entry to whatever directory contains the detours.lib file (e.g. "MyProject/Libs/MSDetour" ... where MSDetour is a folder with the "detours.lib" in it) and then Visual Studio should find the library and link everything correctly.
As a side note, if you are using the Visual Studio developer console for building your project/solution you might want to look into CMake ... it is, in my opinion, significantly easier to work with (less "settings" digging) and maintain in the long-run.

Create MS Visual C++ DLL project out of existing sources

My goal is to compile existing C++ classes (legacy code, stored in a set of *.h files) into a DLL so that it can be further integrated into a C# application.
For that purpose, it seems best to use MS Visual Studio. I have no experience with this environment, so I tried the naive approach found on MSDN and other SO answers:
File | New | Project from existing code
selected Visual C++
selected file location that is base for include references used in those .h files
specified a project name
let the wizard find and add all C++ files below the directory
selected "Use Visual Studio" for build, with project type "Dynamically Linked Library (DLL) project"
checked none of the checkboxes below (ATL, MFC, CLR)
specified . dir in the "Include search paths (/I)" in Debug settings
checked "Same as Debug configuration" in "Release settings"
clicked Finish button
This creates couple of VS files in the directory:
mylibrary.sln
mylibrary.vcxproj
mylibrary.vcxproj.filters
mylibrary.vcxproj.user
With a project created this way, I press F6 or select Build | Rebuild solution from the menu.
Then I expect the build to produce the .dll file somewhere, but it does not appear. Only these files appear:
.vs/mylibrary/v15/.suo
.vs/mylibrary/v15/Browse.VC.db
.vs/mylibrary/v15/Browse.VC.opendb
.vs/mylibrary/v15/ipch/AutoPCH/efad7c74cd39331b/EXAMPLE.ipch
Debug/mylibrary.log
Debug/mylibrary.tlog/mylibrary.lastbuildstate
Next, I decided to try creating a fresh new library project, just to observe the differences to get some hints, but that did not help - there were too many differences, even in the file structure...
My questions are:
is my choice of MS Visual C++ a good one for given purpose?
if so, what am I doing wrong here?
I think your steps are probably correct and I think that the right approach to use the code from a C# application. You definitely can call a C++ library from C# by importing the methods.
You missed only to export the methods that you want to use from your library. try using __declspec(dllexport) with these methods. please check this link:
https://msdn.microsoft.com/en-us/library/a90k134d.aspx.
Also, the output should be at the build folder, not the source code folder
Compiling .h files into libraries is ok, the compiler does not care - however, the UI does.
Still, you can tweak this by directly editing the .vcxproj file.
While doing so, make sure that the <ClCompile> sections contain:
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
Note that you can use commandline for building the DLL project:
"%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" -target:Clean,Build
(this assumes that your current directory is the one with your .vcxproj)

SDK Implementation lib/inc/dll

I'm using Microsoft Visual Studio and an SDK to develop an app. The SDK includes the files listed above. I'm coding in C++. I'm not sure what I need to include to be able to use the SDK.
I did the things below and tried building a simple hello world app but I keep getting the error LNK2019: unresolved external symbol_WinMain#16 referenced in function __tmainCRTStartup
What I've Done:
1)Right Click-Properties-Linker-Additional Library Directories-Changed path to where I have the lib folder on my computer.
2)Same but added the Inc folder that came with the SDK to C/C++ Additional Include Directories.
By doing the above I assume I am linking the lib and .h files to the project that came with the SDK. The SDK also came with .dll files which I have done nothing with. You can use a few different languages with the SDK so I'm not sure what I need to use and not use.
However, the header files are not showing up under external dependencies.
I guess I should note that I have tried to run it a couple times before this and am not sure if that could be causing an issue building it.
Thanks.
The error you're seeing has nothing to do with header files or source code. The error is due to you not choosing the correct Visual Studio project.
If you are writing a console program, and you defined a main() function as the entry point, then you should be choosing a Win32 Console Application project from Visual Studio.
The error stems from the program looking for the WinMain function, and that function exists when you choose a Win32 Project. Usually these projects are for building GUI-based Win32 apps.

How do I know if a project is MFC application

My friend has given me a visual studio project , with lots of files.
I know for sure that it is C++ application.
But I want to verify that if it is an MFC application or not.
The issue is since this project has been developed on a higher version (VS 2013) than mine (VS 2010), it is not opening in VS 2010.
So I thought I will make a new project and then gradually add these files.
But When I try to make a new project , I have many options to choose , such as MFC, win32 etc.
I guess it is an MFC application. But to be sure I want to verify that this project is indeed an MFC application. How do I do this ? Especially by just looking at the project files!
Look into your source files and check if afx.h or afxwin.h is included any where.
Project Settings are secondary. Only if such a Header file is used in the Project the MFC libraries are included in the link phase.
You would probably be better off checking the source files, not the project file. For sure, the MFC library need to be present in the project file, but that option could have been left on accidentally.
You could search the source files for CWinApp, which is the class MFC applications need to be derived from. Also, you could try this page on MSDN, which will give you some idea about source and header files typically found in an MFC project.
VS2010 and VS2013 uses XML for .sln/.vcxproj files. Just create a minimal solution in VS2010. Then use a good text editor or even a file comparer to adjust settings inside the .vcxproj.
Great syntax changes occurred between VS2008 and VS2010. But since then most XML tags were kept unchanged between VS2010 and VS2013.

Using Boost C++ unit tests with visual studio and can't find .cpp files in other project

I have a solution where I've added and setup boost unit tests. The problem is that I have another project I'd like to test that has some classes in it. In fact, that project is the main reason I added boost.
My project that needs testing is set to output as a .dll. And the problem is that, whenever my tests project needs to access code from the other projects, it can access the header just fine. However, if the header has unresolved code in it that's otherwise resolved in a .cpp file of the project with the objects, I receive a linking error. Is there a way around this? I'd ideally like to keep my objects in my other dll and then test them in my tests project.
You're probably not telling your test project where to find the symbols. Either link against your production code's .lib manually, or you can add the project as a reference and VS will link your projects automatically.
Go to your project's properties, under Common Properties choose Framework and References. Click the Add New References... button, and select your other project. Since it's a .dll, you then want to set Link Library Dependencies to False (save and reopen the dialog, that setting seems to be buggy).
The problem was that the visual studio compiler couldn't link to the CPP in the other files. I had to add the CPP files to the boost project as well using the existing files option.