Can't run terminal(Visual Studio) to read c++ inputs - c++

Thats what I see:
igorz#DESKTOP-QKLDJRN MINGW64 /c/Websites/cpp-series
$ cd "c:\Websites\cpp-series" && g++ gigel.cpp -o gigel && "c:\Websites\cpp-series"gigel
bash: cd: c:\Websites\cpp-series" && g++ gigel.cpp -o gigel && c:Websitescpp-series"gigel: No such file or directory
Thats the code(but the problem is'nt in code I think)
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}

Related

Cannot set %PATH% in windows from boost::process

I have a program which works great on Linux, but fails to preserve some of %PATH% on Windows. Here's an example:
parent.cpp
#include <boost/process.hpp>
int main()
{
auto this_env = boost::this_process::environment();
boost::process::environment env = this_env; // make a copy to avoid changing the orig
env["TESTVAR"] = "Test12"; // Confirm we can set a variable
env["TESTVAR"] += "Test34"; // Confirm we can append
env["PATH"] += "C:\\Users\\"; // Confirm we can append to PATH
boost::process::child c(
boost::process::exe = "./child.exe",
boost::process::env = env
);
c.wait();
}
child.cpp
#include <boost/process.hpp>
#include <iostream>
int main()
{
auto env = boost::this_process::environment();
std::cout << "TESTVAR = " << env["TESTVAR"].to_string() << std::endl
<< "PATH = " << env["PATH"].to_string() << std::endl;
}
Output on Linux (good):
$ g++ parent.cpp -o parent.exe
$ g++ child.cpp -o child.exe
$ echo PATH = $PATH
PATH = /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
$ ./parent.exe
TESTVAR = Test12:Test34
PATH = /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:C:\Users\
PATH is augmented! ~~~~~^
Output on Windows (bad):
$ x86_64-w64-mingw32.static.posix-g++ parent.cpp -o parent.exe -lwsock32 -lboost_filesystem-mt-x64
$ x86_64-w64-mingw32.static.posix-g++ child.cpp -o child.exe -lwsock32
$ scp parent.exe child.exe user#windows:
$ ssh user#windows
> echo %PATH%
C:\Windows\system32;C:\Windows;...
> parent.exe
TESTVAR = Test12:Test34
PATH = C:\Users\
^~~~ Path is replaced!
Is this a bug in boost::process, a feature of Windows, or is there something wrong with my code?
There appears some case-sensitivity in windows environment variables. By changing child.cpp to this, we can see all environment variables:
#include <boost/process.hpp>
#include <iostream>
int main()
{
auto env = boost::this_process::environment();
for (auto it : env )
std::cout << it.get_name() << " = " << it.to_string() << std::endl;
}
The output contains:
Path = C:\Windows\system32;C:\Windows;...
TESTVAR = Test12;Test34
PATH = C:\Users\
If we change parent.cpp from :
env["PATH"] += "C:\\Users\\";
to
env["Path"] += "C:\\Users\\";
we get the correct output on Windows:
Path = C:\Windows\system32;C:\Windows;...;C:\Users\
TESTVAR = Test12;Test34
While cmd.exe can set/get path insensitively (SET PATH=%PATH%;C:\\Users\\), the API used by boost is case-sensitive. Strange!

mingw compiled executable does not output characters in powershell/cmd

I tried to use the following code to output files in current directory
#include <filesystem>
#include <iostream>
using namespace std::filesystem;
int main() {
for (directory_iterator next("."), end; next != end; ++next) {
std::cout << next->path() << std::endl;
}
return 0;
}
the compile command is a simple g++ demo.cpp -o demo.exe, the path to g++ is C:\msys64\ucrt64\bin\g++.exe.
It worked properly when i ran it in bash (msys2),
$ ./demo.exe
".\\.clangd"
".\\.vscode"
".\\demo.cpp"
".\\demo.exe"
but when i did the same thing in powershell or cmd, it output nothing.
PS C:\Users\cnjawi> .\demo.exe
PS C:\Users\cnjawi>
The following code could run just fine in powershell and cmd.
#include <filesystem>
#include <iostream>
using namespace std::filesystem;
int main() {
std::cout << "test\n";
return 0;
}
However, if i add the original code, the issue occurs, even the "test" couldn't be output.
#include <filesystem>
#include <iostream>
using namespace std::filesystem;
int main() {
std::cout << "test\n";
for (directory_iterator next("."), end; next != end; ++next) {
std::cout << next->path() << std::endl;
}
return 0;
}
Thanks to HolyBlackCat's comment. When i double-clicked this program, Windows prompted The procedure entry point ~ could not be located, which gave the key info -- the program doesn't actually work in powershell due to some missing DLLs, rather than working but not outputting.
However, a collapsed program does not issue warnings in powershell, which made it appear to have worked "normally".
The simplest way to solve it is using the static library. For this instance, use g++ -c demo.cpp to generate demo.o and then g++ -static demo.o /ucrt64/lib/libstdc++fs.a -o demo.exe(in bash).

GCOV/LCOV ignores `used functions` declared before an `unused function`

The question may seem weird.
I tried to use GCOV/LCOV for my small project and practiced with simple code before applying it. While practicing it, I encountered an error that I had no idea how to solve.
The report created by LCOV showed that the functions declared before the unused function inside the source code file are reported as unused functions. In contrast, the functions displayed correct outputs when the binary file was executed.
The followings are actual codes used for the practice.
# makefile
CC = g++
CFLAG = -g -fPIC -fprofile-arcs -ftest-coverage
RM = rm -rf
main.o : main.cpp
$(CC) $(CFLAG) -c -Wall -Werror main.cpp
dummy_class.o : dummy_class.cpp
$(CC) $(CFLAG) -c -Wall -Werror dummy_class.cpp
build : main.o dummy_class.o
$(CC) $(CFLAG) -o main main.o dummy_class.o
gcov: main.cpp dummy_class.cpp
gcov main.cpp dummy_class.cpp
coverage.info: gcov
lcov --capture --directory . --output-file coverage.info
lcov -remove coverage.info "/usr/include/*" "/usr/local/include/*" --output-file coverage.info
report : coverage.info
genhtml coverage.info --output-directory ./out
clean :
rm -f main
rm -f *.o *.so *.gcno *.gcda *.gcov coverage.info
rm -r out
do :
make build
./main
make report
// dummy_class.hpp
#pragma once
void func_even_case(void);
void func_odd_case(void);
void func_not_reachable(void);
void dummy(void);
void dummy2(void);
void dummy3(void);
// dummy_class.cpp
#include <iostream>
#include <vector>
#include "dummy_class.hpp"
void func_even_case(void)
{
std::cout << "This is even case" << std::endl;
}
void func_odd_case(void)
{
std::cout << "This is odd case" << std::endl;
}
void func_not_reachable(void)
{
std::cout << "This is not reachable" << std::endl;
}
void dummy(void)
{
std::cout << "This is dummy1." << std::endl;
}
void dummy2(void)
{
std::cout << "This is dummy2." << std::endl;
}
void dummy3(void)
{
std::cout << "This is dummy3." << std::endl;
}
// main.cpp
#include <iostream>
#include "dummy_class.hpp"
int main(void)
{
for (int i = 0; i < 10; ++i)
{
if (i % 2 == 0)
{
func_even_case();
}
else if (i % 2 != 0)
{
func_odd_case();
}
else
{
func_not_reachable();
}
}
func_not_reachable();
dummy();
dummy2();
dummy3();
return 0;
}
When func_not_reachable() is placed outside the for-loop, the report returns
Overall coverage rate:
lines......: 96.7% (29 of 30 lines)
functions..: 100.0% (7 of 7 functions)
and the result is expected.
When func_not_reachable() is removed, the expected result was
Overall coverage rate:
lines......: 86.5% (25 of 29 lines)
functions..: 100.0% (6 of 7 functions)
since func_not_reachable() is the one that will not be executed.
However, the actual result was
Overall coverage rate:
lines......: 65.5% (19 of 29 lines)
functions..: 57.1% (4 of 7 functions)
If the dummy_class.cpp is modified as following
#include <iostream>
#include <vector>
#include "dummy_class.hpp"
void func_even_case(void)
{
std::cout << "This is even case" << std::endl;
}
void func_odd_case(void)
{
std::cout << "This is odd case" << std::endl;
}
void dummy(void)
{
std::cout << "This is dummy1." << std::endl;
}
void dummy2(void)
{
std::cout << "This is dummy2." << std::endl;
}
void dummy3(void)
{
std::cout << "This is dummy3." << std::endl;
}
// unused function declared at the end of the source code.
void func_not_reachable(void)
{
std::cout << "This is not reachable" << std::endl;
}
The report result becomes follows.
Overall coverage rate:
lines......: 34.5% (10 of 29 lines)
functions..: 14.3% (1 of 7 functions)
I am sure I made errors while using GCOV and LCOV, but I cannot figure out where I made a mistake.
Can someone tell me where I made a mistake?
The above code was executed on the following.
Ubuntu 20.04.4 LTS
g++ (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
gcov (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
lcov: LCOV version 1.14
Find out the source of the problem.
The problem was related to the compiler (g++ 11.1.0) or gcov (9.3.0) I used.
The code coverage reported the correct result when the compiler changed from g++ to clang.
To ensure the problem is related to the specific version of the compiler (g++ 11.1.0), I upgraded the system from Ubuntu 20.04.4 LTS to Ubuntu 22.04.4 LTS.
The test condition became as follow
Ubuntu 22.04.4 LTS
g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
gcov (Ubuntu 11.2.0-19ubuntu1) 11.2.0
After the update had been made, the code coverage reported the correct result with g++.

std::ifstream::open inconsistent failure

I was working on a project involving fstream when I ran into this inconsistency. My project is failing to open a .txt file and in attempting to debug it I created test.cpp which, as far as I can tell, is functionally identical to my main.cpp, however they output differently when compiled and run.
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
double get_pe(double price, double rent);
int main()
{
std::ifstream ifile;
std::string text;
std::vector<int> price;
std::vector<int> rent;
// ------------------------------ Problem 1 ------------------------------ \\
ifile.open("test.txt", std::ios::in);
if(ifile.is_open())
{
std::cout << "Works\n";
ifile.close();
}
else
{
std::cout << "Fails\n";
return 1;
}
return 0;
test.cpp
#include <iostream>
#include <fstream>
int main()
{
std::ifstream ifile;
ifile.open("test.txt", std::ios::in);
if(ifile.is_open())
{
std::cout << "Works\n";
ifile.close();
}
else
{
std::cout << "Fails\n";
return 1;
}
return 0;
}
Console Output
$ g++ main.cpp | g++ test.cpp -o t.out
$ ./a.out
Fails
$ ./t.out
Works
$ ls
a.out housingPriceAndRent.txt main.cpp streetAdresses.txt test.cpp test.txt t.out
$ g++ --version
g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
I'm really interested in what's causing this, but I haven't the slightest clue.
Your issue is here:
// ------------------------------ Problem 1 ------------------------------ \\
Looks pretty benign right? Just a comment? But the problem is that escape character at the end. That's your way of saying "ignore this linebreak and treat the next line as if it was on this one."
So the next line:
ifile.open("test.txt", std::ios::in);
is actually part of that comment!! You never run that ifile.open()--so of course ifile.is_open() will be false!
I would expect any IDE worth its salt to have colored this this properly so you could quickly see that that line was a comment (that's how I caught it). Notice how for me, line 18 looks green like a comment:

Parsing LLVM IR from bitcode file

I am trying to parse LLVM IR from a bit code file. I went through the following steps.
hello.cpp
#include <iostream>
int main() {
std::cout << "Hello world!" << "\n";
return 0;
}
dump.cpp
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
using namespace llvm;
int main()
{
LLVMContext context;
SMDiagnostic error;
Module *m = parseIRFile("hello.bc", error, context).get();
if(m)
{
m->dump();
}
return 0;
}
$ clang++ -O3 -emit-llvm hello.cpp -c -o hello.bc
$ clang++ -g -O3 dump.cpp llvm-config --cxxflags --ldflags --system-libs --libs all -o dump
$ ./dump
Assertion failed: (Val && "isa<> used on a null pointer"), function doit, file /Users/chamibuddhika/Builds/llvm/include/llvm/Support/Casting.h, line 106.
Abort trap: 6
So I get the above error at the end. What may be causing this? I am on llvm-6.0 rc2.