Compiling a cpp file on linux with Windows libraries - compilation

I would like to solve this issue once for all, what is the best best to compile a .cpp file that uses windows libraries (to create a exe file).
For instance I have this cpp starting with:
#include "stdafx.h"
#include <Windows.h>
And I get
stdafx.h: No such file or directory
Windows.h: No such file or directory
I know for instance that stdafx require Visual C++ on Windows, but I want to compile it on Linux, how would you do ?
Thanks a lot

The short answer is you cannot build a Windows executable on Linux. But you can download the free Visual Studio Community Edition to build it on Windows.
stdafx.h is a header file in your project. It is used by Visual Studio's pre-compiled headers feature. If you use a predefined project template, Visual Studio will auto-generate stdafx.h and mark it for pre-compilation. You then include the common C++ headers, e.g. STL, in stdafx.h and include stdafx.h in each of your source code files.
When you are not using Visual Studio stdafx.h is a convenient place to pull in the standard headers for runtime libraries but serves no other purpose.
windows.h is the header file for Windows runtime APIs. The Windows APIs and hence the headers are not available on Linux. If you want to build an executable on Linux to run on Linux then you must replace Windows APIs with the Linux equivalents.

Related

Compiling C++ files during runtime using Visual Studio compiler

I'm trying figure out how to compile C++ code from an executable during runtime using Visual Studio compiler under Windows.
I'll be using Visual Studio IDE to build main project into an executable and use CreateProcess to compile other C++ files and create a DLL to later load/use/unload this DLL.
I understand that one way of doing this requires setting environment variables(mainly PATH, INCLUDE and LIB) and there's a .bat file called "vcvarsall.bat" which does this.
The part I'm stuck with is the argument(s) passed to this batch file. I see that first argument is the platform with some of the options being x86, amd64, arm, etc. But how do I programmatically figure out which one of these arguments I should be using considering main executable could've been built with any one of these?
You can prepare a regular solutionfor this purpose, containing one project with a single file, and use it to compile your file easily.
Now, all you need is to reame your file to the file name in the project and compile a solution with command line. Alternatively, you can also edit the project and replace the existing filename with your file name.
To do so you need to resolve the environment variable %DevEnvDir% and run the folowing command with the platform name (x64, win32 etc.) and configuration name(Release or Debug)
like this:
%DevEnvDir%\devenv.com \path\to\yoursolution.sln /ReBuild "Release|x64"

Linking External C++ Library (Exerces) in Visual Studio

I am working on a C++ project with
#include <xercesc/sax2/ContentHandler.hpp>
#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/util/XMLString.hpp>
On the offical xercesc website, they have the following instructions:
I downloaded the distribution file
Opened the xerces-all.sln file in VS2017 and pressed build
Then I opened my project in VS2017, went to properties->Linker->Input->Additional Dependencies and added xerces-c.lib
However the VS2017 complier still can not find the included xerces files
The included documention you are looking at is titled "Building on Windows" so if for beuiding the library, not on using it afterwards.
You need to add the path to the include files in your project in C/C++ -> General ->Additional include directories
And for the library, you may need to add to the Additional Library Directories

Using a DLL with Qt in Visual Studio

I'm trying to use a DLL with a Qt project in Visual Studio. Normally to use a DLL, it is put in the program's executable directory. However, after putting the DLL in just about every directory I could think of, it is still not being used by the program. Is there anything extra that I need to do aside from putting the DLL in the executable directory? Thanks.
The dll should be in the working directory of the executable. This is listed under the "Debugging" section of the Project Properties.
Almost all of the Qt plugin directories need to be in a specific folder next to the exe for it to be found (imageformats, platforms, etc).
Another way to find out if you are in the right directory is to add this line to your main.cpp:
#include <QDir>
#include <QDebug>
qDebug() << "Current Directory:" << QDir::currentPath();
Hope that helps.
PS also look at the order of dll search directories for windows. I'll post more later about it. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx#search_order_for_desktop_applications

How to include png++/libpng for Visual Studio

can anybody help me how to use png++/libpng in a Visual Studio Project?
For Linux there are several tutorials and examples, but for the usage in a Visual studio project I found no help.
My CMakeLists.txt-file looks this way:
# project
cmake_minimum_required (VERSION 2.6)
project (libviso2)
# directories
set (LIBVISO2_SRC_DIR src)
# include directory
include_directories("${LIBVISO2_SRC_DIR}")
# use sse3 instruction set
SET(CMAKE_CXX_FLAGS "-msse3")
# sources
FILE(GLOB LIBVISO2_SRC_FILES "src/*.cpp")
# make release version
set(CMAKE_BUILD_TYPE Release)
# demo program
add_executable(viso2 ${LIBVISO2_SRC_FILES})
target_link_libraries (viso2 png)
Using CMake GUI and the above CMakeLists.txt-file I can generate a Visual Studio Project that works fine for itself until the png++-algorithms are handled in the code (build error). So basically my problem is that I have no experience in CMake and png++/libpng.
I have two folders downloaded from the web, one folder png++ and one folder libpng.
In these two folders there is all the code for the two libraries.
So my question is:
Could anybody explain to me how to arrange/include these two folders in a Visual Studio project producing no error, this could be a very simple project starting:
#include "png++/png.hpp"
using namespace png;
How would I edit the CMakeLists file in order to build a Visual studio project in which the png++ and libpng folders are correctly included so as to make it feasible to use the png++ code?
Thank You in advance for any help!
png++ is a C++ wrapper for libpng library, so it only has header files.
what you need to do is to compile libpng and its dependency(zlib).
and include header files of png++, libpng and zlib, add library of libpng and zlib.
That's all you need, I ran libviso2 successfully on Windows, good luck!

How to add header file of MITK in my CPP program of VS 2010?

I have compiled and build using CMAke and VS 2010. But now I want to try some basic program using MITk library, but I can't find where is the header file, DLL, Lib file? Same as like in OpenCv when we compile and build it, then in install folder we have bin folder that contain DLL file, Lib folder contain lib file and include folder contain header file of CPP program.
So, where is the header file of MITK build library ?
If anybody have idea then suggest to me.
It seems you want to use MITK as a toolkit. As the documentation suggests, you can (must?) use cmake. The generated VS2010 .sln file will handle all the include and library files for you. Use the .bat file if there's one.
There's probably a way to use it without cmake, but I'm pretty sure it's a bad idea.

Resources