I have already set up my Msys2 and installed mingw-w64-x86_64-boost on it.
I provided a minimal example of c++ with boost which I will build using the command g++ main.cpp -o main.exe -lboost_program_options-mt:
#include <boost/program_options.hpp>
#include <string>
#include <iostream>
namespace po = boost::program_options;
int main(int argc, char** argv) {
// Arguments will be stored here
std::string input;
std::string output;
// Configure options here
po::options_description desc ("Allowed options");
desc.add_options ()
("help,h", "print usage message")
("input,i", po::value(&input), "Input file")
("output,o", po::value(&output), "Output file");
// Parse command line arguments
po::variables_map vm;
po::store (po::command_line_parser (argc, argv).options (desc).run (), vm);
po::notify (vm);
// Check if there are enough args or if --help is given
if (vm.count ("help") || !vm.count ("input") || !vm.count ("output")) {
std::cerr << desc << "\n";
return 1;
}
std::cout << "The rest of the code will be here"; <- Indication that it is working
}
It compiles and links without logging an error when I ran the said command, but now when I try to run it, it just doesn't execute properly.
At the least, I was expecting to see the text The rest of the code will be here to be outputted to the console when I ran it as an indication that it is being executed, however it didn't output it:
I tried to debug it, but GDB itself can't debug it
This is what it looks like in the VSCode Debug Console:
Running a separate GBD on the command line:
With all of that said, I am assuming that this is a linker error given that the resulting executable is being outputted by the compiler. What are your thoughts regarding this problem?
Yooo! I fixed the issue.
I first tried to run it on a fresh virtual machine to cross out the possibility that this issue is caused by my current environment. Surprisingly enough, it worked perfectly fine on the virtual machine. Knowing that it only happens in my current environment, I did proceed to make the following changes ().
Complete reinstallation of msys2 in my system, (you can disregard this step because I'm pretty sure that step 2 is enough)
Moving up the priority of the msys2 path in the environment variables.
To do step 2 follow these steps (windows 10):
Search for "Edit environment variables for your account" in the search bar
Under "User variables" select variable "path" and click edit
Now, select the item for the bin path of the mingw on Msys2
Once you've selected it now, move up its priority as shown below.
Press ok, then restart the terminal, and it should work now
Related
Lets take this very simple program here for example:
// test.cpp
#include <string>
#include <iostream>
using namespace std;
int main() {
string str = "Hello";
cout << str << endl;
return 0;
}
now I compile this code with g++ compiler:
g++ -g test.cpp -o test.exe
now I am trying to debug this with gdb:
gdb test.exe
after I set breakpoint on main and then reach the line return 0, I try to see what is in the string str. But I cannot print it in the console. It says <error reading variable>. Not only in gdb console, even Visual Studio Code UI using gdb gives the same output.
Here is a screenshot of my console:
I have searched for this everywhere and the only relevant question I found was this, which did not work.
I also found this post on github VS Code repo issues. The fix suggested there might work I am not sure, I cannot find the setting that he suggested on my Windows 11 machine.
How do I read the value in the string in debug mode?
Edit
After #ssbssa suggested me to update my gcc, I used MSYS2 to get the latest gcc, g++, and gdb versions. Now I have gdb 12.1. Now it is not showing the old error anymore but now it says "Converting character sets: Invalid argument". Still struggling to get it to work.
First run your program with gdb like so:
gdb test.exe
Now inside the command line interface run the command:
set charset UTF-8
This should temporarily fix your problem. The only inconvenience might be that you need to run this line every time you debug on your command prompt with GDB.
I noticed that you are also using Visual Studio Code. You can install C++ extensions for VS Code and there you can add the command set charset UTF-8 in the launch.json setupCommands array as shown here. This way you can debug your application faster.
i have some problems:
i cant run c and c++ on vs code (or, i mean im trying by adding the path of MinGW on env vars, but no results by typing on cmd gdb --version, but i have by typing g++ --version)
aaand i just hided this bar by mistake, how to adjust? thanks to everyone!
1- Add to the path variable the directory of the bin folder of MinGW.
Ex: C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin
2- Check that your Mingw-w64 tools are correctly installed and available, open a new Command Prompt and type:
g++ --version
OR
gdb --version
3- Create helloworld.cpp:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
4- Save
5- From the main menu, choose Terminal > Configure Default Build Task. In the dropdown, which will display a tasks dropdown listing various predefined build tasks for C++ compilers. Choose g++.exe build active file, which will build the file that is currently displayed (active) in the editor.
This will create a tasks.json file:
6- To run the code:
Save helloworld.cpp
press Ctrl+Shift+B or from the Terminal main menu choose Run Build Task
You should see this in the terminal:
Create a new terminal using the CTRL + SHIFT + ~
In that new terminal type: .\helloworld.exe to run the code
Reference: C++ in VS-code
I have this well known c++ program in my Visual Studio 2015 Prof:
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Hello World!" << std::endl;
return 0;
}
As expected, it shows "Hello World!" if I hit Ctrl+F5. However, if I go to the directory within an cmd.exe and execute the HelloWorld.exe file, it doesn't show anything as output, but does quit (I can type in again).
According to a similar question I checked the settings of the project but I did not need to change anything, the Configuration Properties -> Linker -> System -> SubSystem already is at Console (/SUBSYSTEM:CONSOLE).
Flushing the std::cout also didn't help anything. It is reproducable on every freshly created project of VS2015 on my Win 7 64-bit machine and seems to be presistent after rebooting.
What is wrong with my IDE / settings?
After the hint of #Scheff I threw the exe file into "Dependency Walker". It gave me a missing UCRTBASED.DLL and lots of second and third level missing dlls (I think this is usually the case on every application?).
I somehow think my (recently installed) anti virus did interfere with that, because it shows some messages in the log regarding my HelloWorld.exe.
However, deinstalling anti virus and restarting machine did the trick. I can finally see Hello World! on cmd.exe.
How can I change the output directory in Google glog?
I only found google::SetLogDestination(google::LogSeverity, const char* path)
tried it with:
google::SetLogDestination(ERROR, "C:\\log\\error.log);
google::InitGoogleLogging("Test");
LOG(ERROR) << "TEST";
but nothing was written!
Btw.: if you suggest another lightweight, easy to use and thread safe library please let me know!
Thx for any help!
You can also do one of the following:
Pass the log directory as a commandline argument as long as you have the GFlgas library installed:
./your_application --log_dir=/some/log/directory
If you don't want to pass it in the commandline and instead set it in the source:
FLAGS_log_dir = "/some/log/directory";
If the Google gflags library isn't installed you can set it as an environment variable:
GLOG_log_dir=/some/log/directory ./your_application
Here is the test what I did, you may try it,
#include <glog/logging.h>
using namespace std;
int main(int /*argc*/, char** argv)
{
FLAGS_logtostderr = true;
google::SetLogDestination(google::GLOG_INFO,"c:/lovelyGoogle" );
google::InitGoogleLogging(argv[0]);
LOG(INFO) << "This is INFO";
LOG(WARNING) << "This is WARNING";
LOG(ERROR) << "This is Error";
system("pause");
return 0;
}
Tested under Visual studio 2012, google-glog 0.3.3 on Windows 7.
It generated lvoelyGoogle20131016-141423.5160 on my C driver.
If you set FLAGS_logtostderr = false, the log file will not be generated,
I believe you have already read this (well, I have no comment on it)
hope this helpful, good luck.
PS: I have tested on QtCreator (Qt5.1) as well on Windows7, nothing output. I have no idea how to fix it now.
I use this:
fLS::FLAGS_log_dir = "c:/Documents/logs";
In my terrifying experience with this library I came to see that this flag FLAGS_log_dir and this function google::SetLogDestination() compete with each other. Wonderful.
I learned that you can use either but not both.
option 1: use flags
FLAGS_log_dir=/path/to/your/logdir
google::InitGoogleLogging(exec_name.c_str());
and generate a bunch of files named your_exec.some_number.machine_name.log.log_severity.... and their respective symbolic link inside the /path/to/your/logdir. A pair of files will be generated for each log_severity you have used in your program (INFO, WARNING, ERROR, FATAL). Fun fact: INFO files contain everything, WARNING files contain everything from warning downwards, and so on. God knows why these symlinks are needed.
option 2: use file names
std::string log_dir = "/path/to/log/dir";
for (int severity = 0; severity < google::NUM_SEVERITIES; ++severity) {
std::string fpath = fs::path(fs::path(log_dir) / fs::path(exec_name).filename());
google::SetLogDestination(severity, fpath.c_str());
google::SetLogSymlink(severity, "");
}
google::InitGoogleLogging(exec_name.c_str());
where fs is the filesystem library of c++17. This is send all logs to the same file (just one, not many) and finally remove that annoying symbolic link.
I want to write a C++ program in Xcode on my Mac. I wrote this basic one to test it. When I build, I get a "Build Successful" message.
However, I cannot run it! If I hit Command+R nothing happens. When I go to Project->Run the Run option is disabled.
#include <iostream>
using namespace std;
int main()
{
cout << "helo" << endl;
}
Launch XCode
In the "Choose template" box, pick Mac OS X, then Command Line Tool. Press Next
Give your project a name, select C++ as the type
You should see a new project with main.cpp
press the Run button
At the bottom of the screen, under All Output you should see:
Hello, World!
Program ended with exit code: 0