gtkmm delays output via stderr stdout - c++

hy,
I found out, that gtkmm is delaying the output I wrote to std::cerr/std::cout until the Gtk::Window is closed.
But after a bit of researching I found out, that this is not always true. For example, if I add a Button to the Window, and I show the button, outputs on STDOUT and STDERR are working as exepcted.
To test this, I have written an MWE: (If you uncomment the line _button.show();, you would see, that output is not delayed. But if you don't show the button, the output is delayed, until the window is closed.)
Also I have addedd a ofstream, which outputs the contents in both cases directly to /tmp/test.
#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/application.h>
#include <fstream>
#include <iostream>
class window_t : public Gtk::Window {
public:
window_t(void);
private:
Gtk::Button _button;
};
window_t::window_t(void): _button("Click ME") {
add(_button);
//_button.show();
std::cerr << "construct-cerr" << std::endl;
std::cout << "construct-cout" << std::endl;
std::cout.flush();
std::cerr.flush();
std::ofstream file("/tmp/test");
file << "construct-file" << std::endl;
file.close();
}
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create(argc, argv, "com.example.test");
window_t gtkmm_window;
return app->run(gtkmm_window);
}
You can compile this code by:
g++ main.cpp -o main `pkg-config --cflags --libs gtkmm-3.0` -Wall -pedantic -Wextra -Werror -Wcast-qual -Wcast-align -Wconversion -fdiagnostics-color=auto -g -O2 -std=c++11
Why does gtkmm delay outputs to STD(OUT|ERR) and how can I prevent this?
I am using gcc version 4.9.2 and gtkmm 3.14.

Related

Unable to initialise sdl2

Im trying to check if sdl is properly installed on my Ubuntu 20.04 and its not.
Running all on 64bit type.
This is my code:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
bool initSDL()
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cout << "Failed to init sdl: " << SDL_GetError() << std::endl;
return false;
}
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
{
std::cout << "Failed to init sdl_image: " << IMG_GetError() << std::endl;
return false;
}
return true;
}
int main(int argc, char *argv[])
{
std::cout << "Code Starting..." << std::endl;
initSDL();
int winX = 900, winY = 600;
std::cout << "Code Exited Properly." << std::endl;
return 0;
}
And this is my output:
Code Starting...
Failed to init sdl: No available video device
Code Exited Properly.
Im running my code with this commands:
gcc -c src/*.cpp -I include -m64 -lstdc++ -std=c++11
gcc *.o -o out/main -lSDL2main -lSDL2 -lSDL2_image -m64 -lstdc++ -std=c++11
./out/main
My code strucure is this way:
Main directory 'sdl_hello_world'
->src -> contains 'main.cpp'
->out -> contains compiled 'main'
Some methods i tried was:
export DISPLAY=:0
export SDL_VIDEODRIVER=x11
So how do i fix this ?
EDIT: I also followed this tutorial (https://www.youtube.com/watch?v=P3_xhDIP7bc&list=PLvv0ScY6vfd-p1gSnbQhY7vMe2rng0IL0&ab_channel=MikeShah), and still it gave the same error. So i guess the problem is in some computer setting.
EDIT: SOLVED:
i followed almost all tutorials and fix methods on the internet to solve my issue which uses gcc in linux. Some solutions on stack overflow seemed to prefer to compile the source code on their own computer WHICH WORKED.
So im pretty sure the way ubuntu installs it on my computer/ idk im new to using sdl. Updating this as might come handy to anyone else.
Still curious, is there a chance i did something wrong, or could this be considered a bug ?
EDIT: followed this btw https://wiki.libsdl.org/Installation

Embedding gnuplot 5 window into Gtkmm 3

I am trying to embed the gnuplot window into my application using the socket/plug concept in gtkmm 3 library. I have followed the example in the official page here and everything works as expected.
Then I moved to embedding gnuplot window. I modified the socket.cpp as follows:
#include <iostream>
#include <fstream>
#include <gtkmm.h>
#include <gtkmm/socket.h>
using namespace std;
class MySocketWindow : public Gtk::Window
{
public:
MySocketWindow()
{
auto socket = Gtk::manage(new Gtk::Socket());
add(*socket);
cout << "Socket id is: " << hex << socket->get_id() << endl;
show_all();
}
};
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example.socket");
MySocketWindow win;
app->run(win);
return 0;
}
I compile/build the code with:
g++ --std=c++11 socket.cpp -o socket `pkg-config gtkmm-3.0 --cflags --libs`
And run it. A black socket window appears with Socket id is 3e0000b message printed on the terminal.
.\socket
Then I run gnuplot in x11 terminal with the corresponding window id above:
Now when I plot sin(x) in gnuplot, I am expecting the socket window to show the plot, but nothing happens. What I am doing wrong here?
I am running Ubuntu 16.04, 64-bit.
I found the cause of the problem on gnuplot's site - see my original question. Now it's left to find how to fix it on the socket's site.

boost program_options custom parsing

Is there a way to have something like
myapp hostname:port
parsed by boost program_options? I'm also using other options and I would love to use boost program_options without having to roll my own parser for argc/argv.
I tried with some combinations of
desc.add_options()
("help", "list all available options")
(new MyCustomValue(&store_var), "")
but it didn't work
As Dan MaĊĦek writes, this looks more fitting for a positional argument. Since you specify a specific structure for the option, though, you might want to add std::regex_match.
Say you start with
#include <string>
#include <iostream>
#include <regex>
#include <boost/program_options.hpp>
using namespace std;
using namespace boost::program_options;
int main(int argc, const char *argv[]) {
try {
options_description desc{"Options"};
desc.add_options()
("help,h", "Help screen")
("ip_port", value<std::string>()->required(), "ip:port");
positional_options_description pos_desc;
pos_desc.add("ip_port", -1);
command_line_parser parser{argc, argv};
parser.options(desc).positional(pos_desc).allow_unregistered();
parsed_options parsed_options = parser.run();
variables_map vm;
store(parsed_options, vm);
notify(vm);
const auto ip_port = vm["ip_port"].as<string>();
At this point, we have the user's input for ip_port. We can define a regex to match it. Note that first part can be a string (e.g., localhost), but the second part must be an integer:
const regex ip_port_re("([^:]+):([[:digit:]]+)");
smatch ip_port_match;
if(!regex_match(ip_port, ip_port_match, ip_port_re))
throw validation_error{validation_error::invalid_option_value, ip_port, "ip_port"};
cout << "ip: " << ip_port_match[1] << " port: " << ip_port_match[2] << endl;
}
catch (const error &ex) {
cerr << ex.what() << '\n';
}
}
When running it, it looks like this:
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out 127.0.0.1:30
ip: 127.0.0.1 port: 30
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out localhost:30
ip: localhost port: 30
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out localhost:30d
the argument for option 'localhost:30d' is invalid
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out
the option '--ip_port' is required but missing
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out localhost:30 foo
option '--ip_port' cannot be specified more than once

c++ hello world throws Segmentation fault - MinGW, Netbeans

Set up MinGW and Netbeans. Only working withe othere IDE's and compilers in before. My machine is x64 win7x64. I'm not shure if I only installed the 32bit version of MinGW.
Compiling a simpel Helle World in x64 leeds to:
g++ -m64 -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d"
-o build/Debug/MinGW-Windows/main.o main.cpp
//Message: sorry, unimplemented: 64-bit mode not compiled in
The win32 Version compiles fine.
// ---
g++ -m32 -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d"
-o build/Debug/MinGW-Windows/main.o main.cpp
// Compiles without any error...
Starting the 32-Version it prints only one message and terminats withe a segmentation fault before returning from first std:cout. Using ftream, to make a file output same error occures.
The demo-Programm of netbeans "Welcome" throws the same exceptions.
#include <cstdlib>
#include <iostream> // EDIT! (worked at this post long time sry.)
using namespace std;
int main(int argc, char** argv) {
int i = 0; // for making shure, program is working before calling fs.open
std::cout << "hello world blubb" << endl; // this one shows up its message at consol.
//programm aborded.
std::cout << "Now again" << endl; // this on is never reached.
return 0;
}
Errormessages:
Signal received: SIGSEGV ( Segmentation fault )
For programm cppapplication_1 pid 7972
You may discard...
Errormessage windows:
cppapplication_1.exe does not work.
Where can I start my serach? Compiler? some dlls? Dependency Walker?
Thx for any comment =).
Cutton

clang try catch not working on OSX

$clang --version
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
magick_wrapper.cpp
#include <Magick++.h>
#include <iostream>
#include <stdexcept>
#include "magick_wrapper.h"
using namespace std;
using namespace Magick;
void hello_world() {
cout << "hello world" << endl;
}
void initialize_imagick() {
static int isInitialized = 0;
if(!isInitialized) {
InitializeMagick(NULL);
isInitialized = 1;
}
}
void throw_exception_test() {
try {
throw runtime_error("just a test");
} catch (exception &error) {
cout << "error occured: " << error.what() << endl;
} catch (...) {
cout << "an exception has rasie" << ", but we don't know what it is" << endl;
}
}
haskell code with ffi
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Foreign.C
import Foreign.Ptr
foreign import ccall "hello_world"
c_hello_world :: IO ()
foreign import ccall "throw_exception_test"
c_throw_exception_test :: IO ()
main = do
c_hello_world
c_throw_exception_test
main.cpp
#include <iostream>
#include "magick_wrapper.h"
using namespace std;
int main() {
cout << "main start" << endl;
throw_exception_test();
cout << "main ends" << endl;
return 0;
}
make file
CC=clang
all: test testc
testc: magick_wrapper.o main.o
$(CC) -o testc main.o magick_wrapper.o `Magick++-config --cppflags --cxxflags --ldflags --libs` -lc++
main.o: main.cpp
$(CC) -c main.cpp
test: test.hs magick_wrapper.o
ghc -v -o test test.hs magick_wrapper.o `Magick++-config --ldflags --libs` -lc++
magick_wrapper.o: magick_wrapper.cpp
$(CC) -c `Magick++-config --cppflags --cxxflags` magick_wrapper.cpp -stdlib=libc++
clean:
rm *o *hi test testc
std output from test (haskell ffi)
$ ./test
hello world
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: just a test
Abort trap: 6
std ouptut from testc (pure c)
$ ./testc
main start
error occured: just a test
main ends
ghc verbose output
*** C Compiler:
clang -m64 -fno-stack-protector -DTABLES_NEXT_TO_CODE -c /var/folders/kp/f2t2ps216tl3p4t6jsplr9400000gn/T/ghc26839_0/ghc26839_4.c -o /var/folders/kp/f2t2ps216tl3p4t6jsplr9400000gn/T/ghc26839_0/ghc26839_5.o -I/usr/local/Cellar/ghc/7.8.3/lib/ghc-7.8.3/include
*** Linker:
clang -m64 -fno-stack-protector -DTABLES_NEXT_TO_CODE -m64 -o test -Wl,-no_compact_unwind test.o -L/usr/local/Cellar/imagemagick/6.8.9-7/lib -L/usr/local/Cellar/imagemagick/6.8.9-7/lib magick_wrapper.o '-lMagick++-6.Q16' -lMagickWand-6.Q16 -lMagickCore-6.Q16 '-lMagick++-6.Q16' -lMagickWand-6.Q16 -lMagickCore-6.Q16 '-lc++' -L/usr/local/Cellar/ghc/7.8.3/lib/ghc-7.8.3/base-4.7.0.1 -L/usr/local/Cellar/ghc/7.8.3/lib/ghc-7.8.3/integer-gmp-0.5.1.0 -L/usr/local/Cellar/ghc/7.8.3/lib/ghc-7.8.3/ghc-prim-0.3.1.0 -L/usr/local/Cellar/ghc/7.8.3/lib/ghc-7.8.3/rts-1.0 /var/folders/kp/f2t2ps216tl3p4t6jsplr9400000gn/T/ghc26839_0/ghc26839_5.o -Wl,-u,_ghczmprim_GHCziTypes_Izh_static_info -Wl,-u,_ghczmprim_GHCziTypes_Czh_static_info -Wl,-u,_ghczmprim_GHCziTypes_Fzh_static_info -Wl,-u,_ghczmprim_GHCziTypes_Dzh_static_info -Wl,-u,_base_GHCziPtr_Ptr_static_info -Wl,-u,_ghczmprim_GHCziTypes_Wzh_static_info -Wl,-u,_base_GHCziInt_I8zh_static_info -Wl,-u,_base_GHCziInt_I16zh_static_info -Wl,-u,_base_GHCziInt_I32zh_static_info -Wl,-u,_base_GHCziInt_I64zh_static_info -Wl,-u,_base_GHCziWord_W8zh_static_info -Wl,-u,_base_GHCziWord_W16zh_static_info -Wl,-u,_base_GHCziWord_W32zh_static_info -Wl,-u,_base_GHCziWord_W64zh_static_info -Wl,-u,_base_GHCziStable_StablePtr_static_info -Wl,-u,_ghczmprim_GHCziTypes_Izh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Czh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Fzh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Dzh_con_info -Wl,-u,_base_GHCziPtr_Ptr_con_info -Wl,-u,_base_GHCziPtr_FunPtr_con_info -Wl,-u,_base_GHCziStable_StablePtr_con_info -Wl,-u,_ghczmprim_GHCziTypes_False_closure -Wl,-u,_ghczmprim_GHCziTypes_True_closure -Wl,-u,_base_GHCziPack_unpackCString_closure -Wl,-u,_base_GHCziIOziException_stackOverflow_closure -Wl,-u,_base_GHCziIOziException_heapOverflow_closure -Wl,-u,_base_ControlziExceptionziBase_nonTermination_closure -Wl,-u,_base_GHCziIOziException_blockedIndefinitelyOnMVar_closure -Wl,-u,_base_GHCziIOziException_blockedIndefinitelyOnSTM_closure -Wl,-u,_base_ControlziExceptionziBase_nestedAtomically_closure -Wl,-u,_base_GHCziWeak_runFinalizzerBatch_closure -Wl,-u,_base_GHCziTopHandler_flushStdHandles_closure -Wl,-u,_base_GHCziTopHandler_runIO_closure -Wl,-u,_base_GHCziTopHandler_runNonIO_closure -Wl,-u,_base_GHCziConcziIO_ensureIOManagerIsRunning_closure -Wl,-u,_base_GHCziConcziIO_ioManagerCapabilitiesChanged_closure -Wl,-u,_base_GHCziConcziSync_runSparks_closure -Wl,-u,_base_GHCziConcziSignal_runHandlers_closure -Wl,-search_paths_first -lHSbase-4.7.0.1 -lHSinteger-gmp-0.5.1.0 -lHSghc-prim-0.3.1.0 -lHSrts -lCffi -liconv -lgmp -lm -ldl
link: done
it seems during unwind, catch code was ignored by some reason. this behavior seems can only repeated under OSX with clang. I have tried gcc under Linux which works perfectly fine.