I have a template class with a static member, for simplicity:
template<typename T>
struct A
{
static inline int x = 1;
};
But if I include the header with struct A definition in several shared-libraries, each of them will have its own instance of A<T>::x. And I would like to have the instance of A<T>::x (for some fixed list of T-types) only in some one shared library, and all other libraries accessing it.
In Visual Studio I can do it by writing in the header
#ifdef _WIN32
# ifdef MY_LIB_EXPORT
# define MY_LIB_API __declspec(dllexport)
# else
# define MY_LIB_API __declspec(dllimport)
# endif
#else
# define MY_LIB_API
#endif
template<typename T>
struct A
{
MY_LIB_API static int x;
};
and in a .cpp-file from the selected dynamic library:
template<> int A<SpecificType1>::x = 0;
template<> int A<SpecificType2>::x = 0;
…
But unfortunately, this way does not work with linux/clang, where in other .cpp-files I get:
error: instantiation of variable 'A<SpecificType1>::x' required here, but no definition is available
Is there a universal way for solving my task?
For GCC you can use:
#define EXPORT __attribute__((visibility("default")))
I've tested it using CMake with the following setup mylib.h:
#define EXPORT __attribute__((visibility("default")))
template<typename T>
struct A
{
EXPORT static int x;
};
mylib.cpp
#include "mylib.h"
template<> int A<int>::x = 1;
template<> int A<bool>::x = false;
main.cpp
std::cout << "A<int>: " << A<int>::x << std::endl;
std::cout << "A<bool>: " << A<bool>::x << std::endl;
with output:
A<int>: 1
A<bool>: 0
CMakeLists.txt
# Change the default to hidden, then you need to explicitly set it
# Thus, normally, you shouldn't have the problem
add_compile_options(-fvisibility=hidden) # only for testing the EXPORT define
add_library(mylib SHARED mylib.cpp)
add_executable(example main.cpp main2.cpp) # Main2 also used mylib
target_link_libraries(example mylib)
Thus, for a universal option I will propose to add this define to your if else define macro list.
I am making a shared library which exports a function that uses a static class instance. The shared library is intended to be dynamically loaded. However, for some reason using the static class instance causes dynamic loading to fail. I am loading the library using Poco.
//my_library.h
#ifndef MY_LIB_H
#define MY_LIB_H
#define DUMMY_MSG 1;
class SendClass
{
public:
SendClass() = default;
SendClass( const SendClass &other ) = delete;
SendClass &operator=( const SendClass &other ) = delete;
static SendClass *get_instance(){ return &singleton_instance; };
int send(){ return DUMMY_MSG; };
private:
static SendClass singleton_instance;
};
extern "C" __attribute__ ((visibility ("default"))) int send();//this is the function to be exported
inline int send()
{
return SendClass::get_instance()->send();
}
#endif // MY_LIB_H
I compile the above header file into a shared library using the command below, and put the library under /tmp/
g++ -shared -fPIC -o libexp.so my_library.h
And then I attempt to load the library in my main program
//main.cpp
#include "Poco/SharedLibrary.h"
using namespace std;
typedef int(*SENDFUNC)();
int main
(
int argc,
char **argv
)
{
Poco::SharedLibrary lib;
lib.load( "/tmp/libexp.so" ); //crashes here!
SENDFUNC send_func = (SENDFUNC)lib.getSymbol("send");
int msg = send_func();
return 0;
}
The program crashes at the line "lib.load( "/tmp/libexp.so" );" with the following message:
terminate called after throwing an instance of
'Poco::LibraryLoadException' what(): Cannot load library
However, if I change the body of SendClass::get_instance to the following, the dynamic loading is completed successfully
//if SendClass::get_instance is implemented as follows, dynamic loading succeeds
static SendClass *get_instance(){ return new SendClass; };
So why does using a static instance cause dynamic loading to fail?
As per #ALX23z's advice I solved the problem by adding the following cpp file
//my_library.cpp
#include "my_library.h"
SendClass SendClass::singleton_instance;
I am trying to create a program that uses plugins in C++ to get some experience with importing and exporting functions from .dll and .so libraries. For simplicity's sake let's only use .dll libraries here.
What I'm trying to do is to make the communication between the plugin and the main program that loaded it "two-way", meaning the main program can call functions from the plugin (this is solved) and the plugin should be able to call functions from the main program (this I'm having trouble with).
I currently am able to create a .dll where I exported the functions with extern "C" {} and __declspec(export).
TestPlugin.h
#pragma once
extern "C" {
__declspec(dllexport) const char* pluginName();
__declspec(dllexport) void onLoad();
__declspec(dllexport) void onShutdown();
}
TestPlugin.cpp
#include "TestPlugin.h"
#include <iostream>
const char * pluginName()
{
return "Test Plugin";
}
void onLoad()
{
std::cout << "onLoad() called!" << std::endl;
}
void onShutdown()
{
std::cout << "onShutdown() called!" << std::endl;
}
I am then loading this test plugin with the following (shortened) code. I removed the error checking and console output.
Plugin.h
#pragma once
#include <filesystem>
#include <iostream>
#include <windows.h>
class Plugin
{
private:
typedef const char*(*pluginNameType)();
typedef void(*onLoadType)();
typedef void(*onShutdownType)();
HINSTANCE m_lib;
public:
Plugin(std::filesystem::path filename);
~Plugin();
pluginNameType pluginName;
onLoadType onLoad;
onShutdownType onShutdown;
};
Plugin.cpp
#include "Plugin.h"
Plugin::Plugin(std::filesystem::path filename)
{
m_lib = LoadLibrary(filename.wstring().c_str());
pluginName = (pluginNameType)GetProcAddress(m_lib, "pluginName");
onLoad = (onLoadType)GetProcAddress(m_lib, "onLoad");
onShutdown = (onShutdownType)GetProcAddress(m_lib, "onShutdown");
}
Plugin::~Plugin()
{
FreeLibrary(m_lib);
}
What I can do now is to call the functions in the plugin (TestPlugin.cpp) from my main program.
main.cpp
Plugin *plugin = new Plugin("pathToDLLGoesHere");
plugin->onLoad();
plugin->onShutdown();
What I would like to do now is to also enable the test plugin I just loaded to have access to functions that are defined in the main program. So let's say in my main.cpp I have something like this ...
main.cpp
int testCall(int val) {
return val + 1;
}
int main()
{
...
return 0;
}
... how would I be able to call the testCall() from the test plugin?
Would it be as simple as to send the function pointer to the plugin and use it? Or do I need to take a different approach here? Thank you for your help!
I have figured out how this works. You can also use extern "C" {} and __declspec(dllexport) to export functions from your main program so the DLLs can see them and when you get the handle of your main program in the DLL, the functions can be called.
In one of your headers in your main program you export the function.
main.h
extern "C" {
__declspec(dllexport) int testCall(int val);
}
main.cpp
int testCall(int val) {
return val + 1;
}
In my test plugin header I created a handle for the main program and a definition for the function I am trying to call from main.
TestPlugin.h
#pragma once
#include <windows.h>
HINSTANCE app;
int(*testCall)(int val);
...
In the body I then assign the handle (calling GetModuleHandle with a nullptr will give you the handle of your program) and then get the exported function from my main program.
TestPlugin.cpp
app = GetModuleHandle(nullptr);
testCall = (int(*)(int val))GetProcAddress(app, "testCall");
After that, I can just call the function.
std::cout << testCall(5) << std::endl;
I am working on a GPS application on the Raspberry pi. I have just installed GPSD by running sudo apt-get install gpsd gpsd-clients. I do get data from my GPS module when I run sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock. My issue resides when I try to make my own GPS app. I have installed libgps-dev
When I try to compile my app I get two errors : undefined reference to gpsmm::~gpsmm() and undefined reference to gpsmm::gps_inner_open(char const*, char const*)
My Main Method:
#include <libgpsmm.h>
int main(int argc, char *argv[])
{
gpsmm gps_rec("localhost", DEFAULT_GPSD_PORT);
return 0;
}
libgpsmm.h:
#ifndef _GPSD_GPSMM_H_
#define _GPSD_GPSMM_H_
/*
* Copyright (C) 2005 Alfredo Pironti
*
* This software is distributed under a BSD-style license. See the
* file "COPYING" in the toop-level directory of the distribution for details.
*
*/
#include <sys/types.h>
#include "gps.h" //the C library we are going to wrap
#ifndef USE_QT
class gpsmm {
#else
#include <QtCore/qglobal.h>
#if defined(LIBQGPSMM_LIBRARY)
# define LIBQGPSMMSHARED_EXPORT Q_DECL_EXPORT
#else
# define LIBQGPSMMSHARED_EXPORT Q_DECL_IMPORT
#endif
class LIBQGPSMMSHARED_EXPORT gpsmm {
#endif
public:
// cppcheck-suppress uninitVar
gpsmm(const char *host, const char *port) : to_user(0) {
gps_inner_open(host, port);
}
#ifdef __UNUSED__
// cppcheck-suppress uninitVar
gpsmm(void) : to_user(0)
{
gps_inner_open("localhost", DEFAULT_GPSD_PORT);
}
#endif
virtual ~gpsmm();
struct gps_data_t* send(const char *request); //put a command to gpsd and return the updated struct
struct gps_data_t* stream(int); //set watcher and policy flags
struct gps_data_t* read(void); //block until gpsd returns new data, then return the updated struct
const char *data(void); // return the client data buffer
bool waiting(int); // blocking check for data waiting
void clear_fix(void);
void enable_debug(int, FILE*);
private:
struct gps_data_t *to_user; //we return the user a copy of the internal structure. This way she can modify it without
//integrity loss for the entire class
struct gps_data_t* gps_inner_open(const char *host, const char *port);
struct gps_data_t _gps_state;
struct gps_data_t * gps_state() { return &_gps_state; }
struct gps_data_t* backup(void) { *to_user = *gps_state(); return to_user; }; //return the backup copy
};
#endif // _GPSD_GPSMM_H_
If anyone can help me figure out my problem, I would be grateful.
The problem is that you are most likely not linking your source code with the library.
You should pass it to your g++ with
g++ -o output sourceFileThatImplementsLibGPS.cpp -lgps
Solution:
Add resource files in the add_executable() statement
Problem
(not in the add_library())
Fail to set main window icon.
Notes:
When I don't use AUTORCC I run into some compilation problems:
QtCore/qglobal.h: no such file or directory. But, I do prefer AUTORCC as a more modern CMake approach.
Without the AUTORCC (different CMakeLists.txt than the provided) and Qt-4.6.2 the current code worked.
different CMakeLists.txt)
Code
This is a minimized code of my project. Tree:
|- CMakeLists.txt
|- main_window.hpp
|- main_window.cpp
|- main.cpp
|- resources
| - resources.qrc
| - images
| - logo.png
main_window.cpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
};
#endif
main_window.cpp
#include "main_window.hpp"
MainWindow::MainWindow()
{
// i tried ":/images.png", ":/resources/images/logo.png", ":/logo.png"
setWindowIcon(QIcon(":images/logo.png"));
}
main.cpp
#include <QApplication>
#include "main_window.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setOrganizationName("Organization");
app.setApplicationName("Application Example");
MainWindow mainWin;
mainWin.show();
return app.exec();
}
CMakeLists.txt.
cmake_minimum_required(VERSION 3.1)
project(qt_project)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt4 4.6 REQUIRED)
set(QT_USE_QTGUI TRUE)
set(QT_USE_QTXML TRUE)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})
// NOTE: it would be more convenient to be able to add the
// resource file here upon the creation of the library
add_library(mylib main_window.cpp)
// SOLVED
// BEFORE: add_executable(qt_test main.cpp)
add_executable(qt_test main.cpp resources/resources.qrc)
target_link_libraries(qt_test
mylib
${QT_LIBRARIES}
)
resources/resources.qrc
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>images/logo.png</file>
</qresource>
</RCC>
Edit
This is the generated qrc_resources.cxx
#include <QtCore/qglobal.h>
static const unsigned char qt_resource_data[] = {
// /users/ddakop/dev/misc/qt/resources/images/logo.png
// ... removed hex data
};
static const unsigned char qt_resource_name[] = {
// images
// ... removed hex data
// logo.png
// ... removed hex data
};
static const unsigned char qt_resource_struct[] = {
// :
0x0,0x0,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x1,
// :/images
0x0,0x0,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x2,
// :/images/logo.png
0x0,0x0,0x0,0x12,0x0,0x0,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x0,
};
QT_BEGIN_NAMESPACE
extern Q_CORE_EXPORT bool qRegisterResourceData
(int, const unsigned char *, const unsigned char *, const unsigned char *);
extern Q_CORE_EXPORT bool qUnregisterResourceData
(int, const unsigned char *, const unsigned char *, const unsigned char *);
QT_END_NAMESPACE
int QT_MANGLE_NAMESPACE(qInitResources_resources)()
{
QT_PREPEND_NAMESPACE(qRegisterResourceData)
(0x01, qt_resource_struct, qt_resource_name, qt_resource_data);
return 1;
}
Q_CONSTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qInitResources_resources))
int QT_MANGLE_NAMESPACE(qCleanupResources_resources)()
{
QT_PREPEND_NAMESPACE(qUnregisterResourceData)
(0x01, qt_resource_struct, qt_resource_name, qt_resource_data);
return 1;
}
Q_DESTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qCleanupResources_resources))
System
CentOS-5, Qt-4.8.6, CMake-3.2.1, gcc-4.8.2
I think you need to link qrc_resources generated file.
I suppose you know the next info:
http://www.cmake.org/cmake/help/latest/manual/cmake-qt.7.html
Where you can see the next line:
add_executable(myexe main.cpp resource_file.qrc)
More info:
http://www.cmake.org/cmake/help/latest/prop_tgt/AUTORCC.html
I'm not sure if this is new or not, but I have figured out a way to add resources to libraries.
In my library's CMakeLists.txt I have the following:
list (APPEND RESOURCES library.qrc)
qt5_add_resources (RCC_SOURCES ${RESOURCES})
...
add_library(library ${RCC_SOURCES} ${SOURCES})
In the library, in one of my "main classes" (for me, this class was the first thing that gets created in the library and the last thing that gets destroyed), I did the following:
class libClass : public QMainWindow {
Q_OBJECT
public:
libClass() : QMainWindow(nullptr, 0) {
Q_INIT_RESOURCE(library);
}
~libClass() {
Q_CLEANUP_RESOURCE(library);
}
};