When I run the executable.\helloworld in VS Code, I get a problem. But it works fine from PowerShell. Could you please guide me to solve the problem?
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
}
'c:\pathtofile\.vscode\extensions\ms-vscode.cpptools-1.13.9-win32-x64\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-gqe1reht.z01' '--stdout=Microsoft-MIEngine-Out-f5jmmmsy.gys' '--stderr=Microsoft-MIEngine-Error-nmujsnhw.lyf' '--pid=Microsoft-MIEngine-Pid-rnomudxw.tbv' '--dbgExe=C:\MinGW\bin\gdb.exe' '--interpreter=mi'
PS C:\pathtofile> .\helloworld
.\helloworld : The term '.\helloworld' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ .\helloworld
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\helloworld:String) [], CommandNotFoundException
Related
I'm using the Boost process header and I can't seem to get the boost::process::system to take in my .cpp file path due to a space in the directory.
auto path = bp::search_path("g++");
int result = bp::system(path, "\"C:\\Users\\Sachin Chopra\\Documents\\rchat\\console_process\\src\\main.cpp\"");
I get the following error when I execute my code:
g++.exe: error: "C:\Users\Sachin: Invalid argument
g++.exe: error: Chopra\Documents\rchat\console_process\src\main.cpp": No such file or directory
g++.exe: fatal error: no input files
The file path formatting works for me .exe file, and will launch from boost if I use it. e.g.
bp::system("\"C:\\Users\\Sachin Chopra\\Documents\\rchat\\build\\console_process\\consoleproc.exe\"");
But when I introduce the g++ path, it seems to mess up. Any help would be appreciated.
Cheers.
You're confusing shell script with the system interface.
You can either use old style, error-prone system:
bp::system(R"(bash -c "echo hello; echo world")");
Or you can pass raw arguments instead of relyng on shell escaping
bp::system(bp::search_path("bash"),
std::vector<std::string>{
"-c",
"echo foo; echo bar",
});
Which could also be written more like
bp::system(bp::search_path("bash"), "-c", "echo 42; echo answer");
In fact, you should probably use the bp::child interace instead of the
system compatible one:
bp::child compiler_job(
bp::search_path("g++"),
R"(C:\Users\Sachin Chopra\Documents\rchat\console_process\src\main.cpp)");
compiler_job.wait_for(5s);
if (compiler_job.running()) {
compiler_job.terminate();
}
int result = compiler_job.exit_code();
std::cout << "compiler_job exit_code: " << result << "\n";
Live Demo
Live On Coliru
#include <boost/process.hpp>
#include <iostream>
namespace bp = boost::process;
using namespace std::chrono_literals;
int main() {
// either use old style, error-prone system
bp::system(R"(bash -c "echo hello; echo world")");
// or pass raw arguments instead of relyng on shell escaping
bp::system(bp::search_path("bash"),
std::vector<std::string>{
"-c",
"echo foo; echo bar",
});
// Which can aslo be written as
bp::system(bp::search_path("bash"), "-c", "echo 42; echo answer");
// in fact, you should probably use the `bp::child` interace instead of the
// `system` compatible one:
bp::child compiler_job(
bp::search_path("g++"),
R"(C:\Users\Sachin Chopra\Documents\rchat\console_process\src\main.cpp)");
compiler_job.wait_for(5s);
if (compiler_job.running()) {
compiler_job.terminate();
}
int result = compiler_job.exit_code();
std::cout << "compiler_job exit_code: " << result << "\n";
}
Prints e.g.
hello
world
foo
bar
42
answer
g++: error: C:\Users\Sachin Chopra\Documents\rchat\console_process\src\main.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
compiler_job exit_code: 1
I am working on Windows and I am trying to write an array into a Ubuntu device using C++ in Visual Studio 2019. Here's a sample of my code:
int Run_WriteCalibTable(char *pcIPAddress, int iNumArgs, float *fArgs, int *iAnsSize, char *sAns)
...
...
...
char pcFolderName[256];
char pcFileName[256];
sprintf(pcFolderName, "%s\\%s",pcSavePath, pcUUTSerialNumber);
sprintf(pcFileName, "%s\\calib_rfclock.conf",pcFolderName);
// WRITE TABLE ON PC
FILE *pFileW;
pFileW = fopen(pcFileName,"wb");
fwrite(&CalibTable, sizeof(char), CalibTable.hdr.v1.u32Len, pFileW);
fclose(pFileW);
}
return 0;
However, I keep having this pop-up from Microsoft Visual C++ Debug Library that says:
Debug Assertion Failed:
Program:...
File: f:\dd\vctools\crt_bld\sefl_x86\crt\src\fwrite.c
Line: 77
Expression: (stream != NULL)
...
I found this thread and I tried logging in as root on my Ubuntu device. I also tried:
mount -o remount,rw /path/to/parent/directory
chmod 777 /path/to/parent/directory
And I can also create/edit manualy any file in the directory I'm trying to write into with my code, but I get the same error when running it.
Anyone knows what could cause this? I think it could be on the Windows side, but I don't know what I am doing wrong. Thanks a lot in advance.
You never check that opening the file succeeds - and it most likely fails, which is why you get the debug pop-up. Your use of \ as directory delimiters may be the only reason why it fails, but you should check to be sure.
I suggest that you use std::filesystem::path (C++17) to build your paths. That makes it easy to create paths in a portable way. You could also make use of a C++ standard std::ofstream to create the file. That way you don't need to close it afterwards. It closes automatically when it goes out of scope.
Example:
#include <cerrno>
#include <cstring>
#include <filesystem>
#include <fstream>
int Run_WriteCalibTable(char *pcIPAddress, int iNumArgs, float *fArgs,
int *iAnsSize, char *sAns)
{
...
// Build std::filesystem::paths:
auto pcFolderName = std::filesystem::path(pcSavePath) / pcUUTSerialNumber;
auto pcFileName = pcFolderName / "calib_rfclock.conf";
// only try to write to the file if opening the file succeeds:
if(std::ofstream pFileW(pcFileName, std::ios::binary); pFileW) {
// Successfully opened the file, now write to it:
pFileW.write(reinterpret_cast<const char*>(&CalibTable),
CalibTable.hdr.v1.u32Len);
} else {
// Opening the file failed, print the reason:
std::cerr << pcFileName << ": " << std::strerror(errno) << std::endl;
}
...
}
I tried to use eigen in mac. After I installing it I run a demo from its' main page in Xcode.The code is as follows:
#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}
But it shows "Eigen/Dense' file not found". I try the ways as followed:
Change include line to:#include <Eigen/Core>
try to place the "Eigen" into the "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/" which includes header file like .
3.use the command line"g++ -I /usr/local/include/eigen3 aaa.cpp -o aaa -O1 ". The third one is from
fatal error: 'eigen3/Eigen/Dense' file not found
But I don't know why use this command line. Can someone tell me why? What's more, It also doesn't work for me.
Some people said "/usr/local/include" is the default folders, but there is no "/usr/local/include" in my Mac. So I make a file folder named "include" in 'usr/local'and put the Eigen into it. But it doesn't work. I can't run this code in Xcode.
Can someone help me to solve this problem? Thanks!
I would like to read some variable's value in Matlab from c++. I searched in Internet and found out below example in Matlab Documentation Page.
for using this example I Did below steps:
I add this include path to project :
c:\program files\Matlab\r2017b\extern\include
then I Add this path Library Directory :
c:\program Files\Matlab\r2017b\extern\lib\win64\microsoft
then I Add this library to project :
"libMatlabEngine.lib"
"libMatlabDataArray.lib"
then I placed needed DLLs beside application EXE file.
then I ran the application after that application faced with access violataion error when startMATLAB() Method has been ran.
Note: I had other problem that I resolved it. but I think that problem was very strange and may be knowing that problem help you to find main reason of my problems.
problem was : when I set dll's files path in environment variables my app didn't find dlls and get "no entry point to *.dll" run time error. but when I copy dlls beside of exe, my app saw them.(I restarted VS2013 after change environment variables.)
#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>
void callgetVars() {
using namespace matlab::engine;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
// Evaluate MATLAB statement
matlabPtr->eval(convertUTF8StringToUTF16String("[az,el,r] = cart2sph(5,7,3);"));
// Get the result from MATLAB
matlab::data::TypedArray<double> result1 = matlabPtr->
getVariable(convertUTF8StringToUTF16String("az"));
matlab::data::TypedArray<double> result2 = matlabPtr->
getVariable(convertUTF8StringToUTF16String("el"));
matlab::data::TypedArray<double> result3 = matlabPtr->
getVariable(convertUTF8StringToUTF16String("r"));
// Display results
std::cout << "az: " << result1[0] << std::endl;
std::cout << "el: " << result2[0] << std::endl;
std::cout << "r: " << result3[0] << std::endl;
}
I use vs2013 and Matlab 2017b in windows 7.
Thanks for your help.
I compiled a c++ source file from the Qt app I am creating. Now I want to run the exe file generated and also to redirect its input and output to txt files. But when I try to run it from QProcess, it fails to execute with exit code -2.
This is how I compiled the file using QProcess -
arguments << fileName << "-o" << exeFileName << "-static";
connect(compileProcess, SIGNAL(finished(int)), this, SLOT(compiled()));
compileProcess->start(QString("g++"), arguments);
And this is how I run the exe from QProcess in the slot compiled() -
runProcess->setStandardInputFile(inputFilename);
runProcess->setStandardOutputFile(QFileInfo(exeFileName).path() + "/output.txt");
int code = runProcess->execute(exeFileName); //code = -2
The program runs fine when I start it manually. So, why can't it be started from QProcess?
I am working with Qt 5.0.2 on Windows 7
This is the source file I am compiling -
#include <iostream>
int main() {
std::string s;s
std::cin >> s;
std::cout << s;
return 0;
}
I finally got it to work. The exe file path had spaces in it and Qt did not implicitly add quotes around it. Adding quotes explicitly did the job.
runProcess->start("\"" + exeFileName + "\"");