How does gcov get the gcda file at runtime? - gcc

I want dumping gcov data at runtime.
I build a libgcov_preload.so using file with the following code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
extern void __gcov_flush();
void sighandler(int signo)
{
printf("prepare invkoe __gcov_flush");
__gcov_flush(); /* flush out gcov stats data */
printf("invkoe __gcov_flush complete");
// raise(signo); /* raise the signal again to crash process */
}
__attribute__((constructor))
void
ctor()
{
struct sigaction sa;
/* setup signal hander */
sa.sa_handler = sighandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGUSR1, &sa, NULL) == -1) {
perror("Could not set signal handler");
}
}
the build command is: gcc -shared -fpic gcov_preload.c -o libgcov_preload.so
and I have a test program as blow:
#include <unistd.h>
#include <stdio.h>
int main()
{
printf("test program started \n");
while(1)
{
sleep(1);
}
return 0;
}
the build command is: gcc -fprofile-arcs -ftest-coverage main_test.c -o main_test
and start main_test with: LD_PRELOAD=./libgcov_preload.so ./main_test
using killall -USR1 main_test send signal to generate gcda file.
but I get error
./main_test: symbol lookup error: ./libgcov_preload.so: undefined symbol: __gcov_flush
I don't know what's wrong.
the gcc version information:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)

Related

PCL viewer inside QtCreator widget with VTK and QVTKOpenGLStereoWidget

I need to make a graphical window with a Qt widget that allows to represent inside it a point cloud that I have previously loaded using the PLC library.
Here's what I have so far that doesn't work (I based it on tutorials and this answer).
I'm using:
Ubuntu 20.04
Qt Creator 5.15
VTK 9.1
PCL 1.12
The reason I am using QVTKOpenGLStereoWidget is that as far as I read both QVTKOpenGLWidget and QVTKWidget are no longer available or will be discontinued in future releases.
Test.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++14
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
MainWindow.cpp
HEADERS += \
MainWindow.h
FORMS += \
MainWindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# INCLUIR LAS LIREARÍAS PARA EL PRROGRAMA
VTK_VERSION = 9.1
PCL_VERSION = 1.12
CONFIG += link_pkgconfig
# LIBREARÍA DE ARCHIVOS DE EIGEN3
PKGCONFIG += eigen3
# LIBRERÍA DE ARCHIIVOS DE BOOST
INCLUDEPATH += /usr/include/boost
# LIBREARÍA DE REPRESENTACIÓN GRÁFICA VTK
INCLUDEPATH += /usr/local/include/vtk-$${VTK_VERSION}
LIBS += -L/usr/local/lib \
-lvtkCommonColor-$${VTK_VERSION} \
-lvtkCommonCore-$${VTK_VERSION} \
-lvtkFiltersSources-$${VTK_VERSION} \
-lvtkInteractionStyle-$${VTK_VERSION} \
-lvtkRenderingContextOpenGL2-$${VTK_VERSION} \
-lvtkRenderingCore-$${VTK_VERSION} \
-lvtkRenderingFreeType-$${VTK_VERSION} \
-lvtkRenderingGL2PSOpenGL2-$${VTK_VERSION} \
-lvtkRenderingOpenGL2-$${VTK_VERSION} \
-lvtkCommonExecutionModel-$${VTK_VERSION} \
-lvtkRenderingFreeType-$${VTK_VERSION} \
-lvtkInteractionStyle-$${VTK_VERSION} \
-lvtkRenderingOpenGL2-$${VTK_VERSION} \
-lvtkRenderingLOD-$${VTK_VERSION} \
-lvtkCommonDataModel-$${VTK_VERSION} \
-lvtkCommonMath-$${VTK_VERSION} \
-lvtkViewsQt-$${VTK_VERSION} \
#-lvtkGUISupportQtOpenGL-$${VTK_VERSION} \
-lvtkGUISupportQt-$${VTK_VERSION}
# LIBRERÍAS PARA EL TRBAJO CON NUBES DE PUNTOS.
INCLUDEPATH += /usr/local/include/pcl-$${PCL_VERSION}
LIBS += -L/usr/local/lib \
-lpcl_common -lpcl_features -lpcl_filters -lpcl_io_ply \
-lpcl_io -lpcl_kdtree -lpcl_keypoints -lpcl_ml \
-lpcl_octree -lpcl_outofcore -lpcl_people -lpcl_recognition \
-lpcl_registration -lpcl_search -lpcl_segmentation -lpcl_stereo \
-lpcl_surface -lpcl_tracking -lpcl_visualization -lpcl_sample_consensus
MainWindow.ui
It only has one widget that has a promote a QVTKOpenGLSTereoWidget
MainWindow.h
I did the initialization of the VTK library, the inclusion of the different library headers and created the PCL viewer and the VTK renderer based on OpenGL.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "pcl/visualization/cloud_viewer.h"
#include <pcl/visualization/pcl_visualizer.h>
#include "pcl/io/pcd_io.h"
#include "pcl/io/ply_io.h"
#include "pcl/io/obj_io.h"
#include "pcl/common/io.h"
#include "vtkCylinder.h"
#include "vtkCylinderSource.h"
#include "vtkRenderWindow.h"
#include <QVTKOpenGLWidget.h>
#include <QVTKOpenGLStereoWidget.h>
#include <QVTKOpenGLWindow.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
using namespace pcl::visualization;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
pcl::visualization::PCLVisualizer::Ptr viewer;
vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
Finally in the constructor of the MainWindow class I simply loaded the file (in this case it was a PLY) and then I tried to make the graphical representation in the widget
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
// Cargar el objeto en dicha nube de putnos en el formato que se desee.
pcl::io::loadPLYFile("/home/alejandro/Documentos/alejandro.ply",*cloud);
viewer->addPointCloud(cloud);
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
//renderWindow->AddRenderer(renderer); THIS MAKES AN ERROR!!
viewer.reset(new PCLVisualizer(renderer, renderWindow, "viewer", false));
ui->widget->setRenderWindow(viewer->getRenderWindow());
ui->widget->update();
}
MainWindow::~MainWindow()
{
delete ui;
}
Main.cpp
I have not changed anything, it is default
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
ERROR.
The main error I have is: /usr/local/include/vtk-9.1/vtkSmartPointer.h:225: error: incomplete type ‘vtkGenericOpenGLRenderWindow’ used in nested name specifier
In file included from /usr/local/include/pcl-1.12/pcl/visualization/point_cloud_geometry_handlers.h:49,
from /usr/local/include/pcl-1.12/pcl/visualization/common/actor_map.h:40,
from /usr/local/include/pcl-1.12/pcl/visualization/pcl_visualizer.h:48,
from /usr/local/include/pcl-1.12/pcl/visualization/cloud_viewer.h:39,
from ../04_pcl_viewer_V3/MainWindow.h:6,
from ../04_pcl_viewer_V3/MainWindow.cpp:1:
/usr/local/include/vtk-9.1/vtkSmartPointer.h: In instantiation of ‘static vtkSmartPointer vtkSmartPointer::New() [with T = vtkGenericOpenGLRenderWindow]’:
../04_pcl_viewer_V3/MainWindow.cpp:16:67: required from here
/usr/local/include/vtk-9.1/vtkSmartPointer.h:225:69: error: incomplete type ‘vtkGenericOpenGLRenderWindow’ used in nested name specifier
225 | static vtkSmartPointer New() { return vtkSmartPointer(T::New(), NoReference()); }
| ~~~~~~^~
I was finally able to find the solution to the problem so I am sharing it as an answer in case it could be useful for someone else.
pclTest_V0.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++14
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
MainWindow.cpp
HEADERS += \
MainWindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# Incluir la librería y la ruta de VTK
VTK_VERSION = 9.1
INCLUDEPATH += /usr/local/include/vtk-$${VTK_VERSION}
LIBS += -L/usr/local/lib \
-lvtkCommonColor-$${VTK_VERSION} \
-lvtkCommonExecutionModel-$${VTK_VERSION} \
-lvtkCommonCore-$${VTK_VERSION} \
-lvtkCommonDataModel-$${VTK_VERSION} \ # Para PCL
-lvtkCommonMath-$${VTK_VERSION} \ # Para PCL
-lvtkFiltersCore-$${VTK_VERSION} \
-lvtkFiltersSources-$${VTK_VERSION} \
-lvtkInfovisCore-$${VTK_VERSION} \
-lvtkInteractionStyle-$${VTK_VERSION} \
-lvtkRenderingContextOpenGL2-$${VTK_VERSION} \
-lvtkRenderingCore-$${VTK_VERSION} \
-lvtkRenderingFreeType-$${VTK_VERSION} \
-lvtkRenderingGL2PSOpenGL2-$${VTK_VERSION} \
-lvtkRenderingOpenGL2-$${VTK_VERSION} \
-lvtkViewsQt-$${VTK_VERSION} \
-lvtkGUISupportQt-$${VTK_VERSION} \
-lvtkRenderingQt-$${VTK_VERSION}
# Incluir el directorio de boost.
INCLUDEPATH += /usr/include/boost
# Incluir el direcotrio de eigen3.
INCLUDEPATH += /usr/include/eigen3
# Incluir las librerías y la ruta de PCL.
PCL_VERSION = 1.12
INCLUDEPATH += /usr/local/include/pcl-$${PCL_VERSION}
LIBS += -L/usr/local/lib \
-lpcl_common -lpcl_io -lpcl_visualization
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "pcl/common/common_headers.h"
#include "pcl/features/normal_3d.h"
#include "pcl/io/obj_io.h"
#include "pcl/io/ply_io.h"
#include "pcl/visualization/pcl_visualizer.h"
#include "pcl/console/parse.h"
#include "QVTKOpenGLNativeWidget.h"
#include "vtkCamera.h"
#include "vtkCubeSource.h"
#include "vtkDataObjectToTable.h"
#include "vtkElevationFilter.h"
#include "vtkGenericOpenGLRenderWindow.h"
#include "vtkNamedColors.h"
#include "vtkNew.h"
#include "vtkPolyDataMapper.h"
#include "vtkQtTableView.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkSphereSource.h"
#include "vtkVersion.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
pcl::visualization::PCLVisualizer::Ptr viewer;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "MainWindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
vtkNew<vtkRenderer> renderer;
renderWindow->AddRenderer(renderer);
// Crear una nube de puntos con los datos almacenados en un archivo
// determinado.
std::string name = "filename.ply";
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::io::loadPLYFile(name,*cloud);
// Generar el visor de la nube de puntos para realizar la visualización.
//viewer.reset(new pcl::visualization::PCLVisualizer("viewer",false));
viewer.reset(new pcl::visualization::PCLVisualizer(renderer, renderWindow,
"viewer",false));
viewer->addPointCloud(cloud);
//ui->Viewer_widget->renderWindow()->AddRenderer(renderer);
ui->Viewer_widget->setRenderWindow(viewer->getRenderWindow());
ui->Viewer_widget->update();
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow.ui
Widget with QVTKOpenGLNativeWidget.h promotion.

Can't GTK3 programs be in multiple source files?

I am trying to make a C-program with multiple c-files in GTK3.
As models I am using:
how-to-split-a-c-program-into-multiple-files
and this
example-0.c
My program looks like this:
Functions.c (with the function activate)
#include "Functions.h"
#include <gtk/gtk.h>
#include "stdio.h"
static void activate (GtkApplication* app, gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_widget_show_all (window);
}
Function.h (the header file)
#include <gtk/gtk.h>
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
static void activate (GtkApplication* app, gpointer user_data);
#endif
Main.c
#include "stdio.h"
#include <gtk/gtk.h>
#include "Functions.h"
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
When I compile, is says:
gcc -Wall `pkg-config --cflags gtk+-3.0` Functions.c Main.c `pkg-config --libs gtk+-3.0`
Functions.c:7:13: warning: ‘activate’ defined but not used [-Wunused-function]
static void activate (GtkApplication* app, gpointer user_data)
^
In file included from Main.c:4:0:
Functions.h:7:14: warning: ‘activate’ used but never defined
static void activate (GtkApplication* app, gpointer user_data);
^
/tmp/ccWzazr0.o: I funktionen "main":
Main.c:(.text+0x38): undefined reference to `activate'
collect2: error: ld returned 1 exit status
and is not compiled!
Yes, GTK programs can be split into multiple C files.
You get an error because you are telling your compiler that the function is only visible within that single C source file:
static void activate (GtkApplication* app, gpointer user_data)
If you want to use that function from another source file, you need to remove static keyword.
Both in the header file as well in the C file you need to remove it.

compiler order with gcc 4.8.5

My gcc version is 4.8.5
posix gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
I have two cpp files:
1.cpp and 2.cpp
while there is a static function in 1.cpp called by 2.cpp。
In most machine,we should like this:
g++ 2.cpp 1.cpp
or it will cause compile error or runtime error。
However,in my machine with gcc 4.8.5,I must compile using “g++ 1.cpp 2.cpp” to make it run successful。
Is this the property of gcc4.8.5? or there is something wrong on my soft,or I used it wrong?
==============================================================
My machine is centos7 installed on virtulbox of mac. Here is my code:
1.h
#include <map>
using namespace std;
class A {
private:
A();
static A _instance;
map<int, int> test_map;
public:
static A& get_instance();
static void fun();
};
1.cpp
#include <iostream>
#include "1.h"
using namespace std;
A A::_instance;
A::A() {
cout << "A::A()\n";
}
A& A::get_instance() {
cout << "A::get_instance()\n";
return A::_instance;
// static A instance;
// return instance;
}
void A::fun() {
cout << "A::fun()\n";
get_instance().test_map[1];
}
main.cpp
#include <iostream>
#include "1.h"
using namespace std;
int test() {
cout << "test()\n";
A::fun();
return 0;
}
int y = test();
int main() {
cout << "main()\n";
A::fun();
}
In most machine and in what I see in the web, we should compile like this:
g++ main.cpp 1.cpp
But in my machine, I must compile like this:
g++ 1.cpp main.cpp
what's wrong with my machine or my gcc?
I think you are facing the static initialization order fiasco which is a classical C++ bug. If initialization function test() in main.cpp is called before constructor for A::_instance has been called, your code will access uninitialized A::_instance::test_map field which is likely to cause segmentation fault. I suggest you rewrite getInstance to create instance when needed:
A *A::_instance;
A& A::get_instance() {
cout << "A::get_instance()\n";
if(!_instance)
_instance = new A;
return *A::_instance;
}
As a side note, I suggest you to use AddressSanitizer to autodetect this and similar types of errors.

g++ 'nullptr' was not declared in this scope

I'm using gcc-4.7.1 on windows 8 Release Preview with git-bash.
$ g++ -v
Using built-in specs.
COLLECT_GCC=c:\Users\nikhil bhardwaj\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=c:/users/nikhil bhardwaj/mingw64/bin/../libexec/gcc/x86_64-w
64-mingw32/4.7.1/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /home/drangon/work/mingw-w64-dgn/source/gcc/configure --host=x8
6_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-nls --enable-languages=c,
c++,objc,obj-c++ --with-gmp=/home/drangon/work/mingw-w64-dgn/build/for_target --
enable-twoprocess --disable-libstdcxx-pch --disable-win32-registry --prefix=/hom
e/drangon/work/mingw-w64-dgn/target --with-sysroot=/home/drangon/work/mingw-w64-
dgn/target
Thread model: win32
gcc version 4.7.1 20120524 (prerelease) (GCC)
When I try to compile a small code snippet,
using namespace std;
struct node
{
int data;
node *left, *right;
};
node *newNode(int data)
{
node *node = new (struct node);
node->data = data;
node->left = nullptr;
node->right = NULL;
return node;
}
I get this error,
$ g++ -I../includes bst.cpp
bst.cpp: In function 'node* newNode(int)':
bst.cpp:13:18: error: 'nullptr' was not declared in this sc
bst.cpp:14:19: error: 'NULL' was not declared in this scope
I'm not able to use either NULL or nullptr, do I need to include some header files?
Try in c++11 mode:
g++ -std=c++11 -I../includes bst.cpp

What is the exact equivalent to LD_PRELOAD on OSX?

I am using LD_PRELOAD to hook a library function, and in Linux it works perfectly. But I cannot figure out how to do the equivalent in OSX.
The setup I have on Linux is as follows:
The code is:
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <ruby.h>
void
rb_raise(unsigned long exc, const char *fmt, ...)
{
static void (*libruby_rb_raise)
(unsigned long exc, const char *fmt, ...) = NULL;
void * handle;
char * error;
if (!libruby_rb_raise) {
handle = dlopen("/path/to/libruby.so",
RTLD_LAZY);
if (!handle) {
fputs(dlerror(), stderr);
exit(1);
}
libruby_rb_raise = dlsym(handle, "rb_raise");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(1);
}
}
// ...code...
return Qnil;
}
Which I then compile with:
gcc -Wall -O2 -fpic -shared -ldl -g -I/path/to/includes/ -o raise_shim.so raise_shim.c
I then execute with:
LD_PRELOAD=./raise_shim.so ruby
All of the above works well on Linux, what is the equivalent for each step to get this working on OSX? I have googled this and have not been able to get it to work with the information provided as the info for some of the steps are missing.
Thanks in advance!
Take a look at DYLD_INSERT_LIBRARIES. That's the variable you're looking for.
See also this answer.

Resources