linux + visual studio 2013 + visualgdb, undefined reference to 'dlopen' - c++

#include <iostream>
#include <dlfcn.h>
using namespace std;
bool LoadEESQuote()
{
void* m_handle;
m_handle = dlopen("libEESQuoteApi.so", RTLD_LAZY);
return true;
}
int main(int argc, char *argv[])
{
LoadEESQuote();
return 0;
}
it is said
need -ldl to compile
, so I set:
project properties-Configuration Properties-NMake-Additional Options: -lrt -ldl
but I still get error.
Please help me

Thanks to n.m.
VisualGDB Project Properties- Makefile settings -Additional linker inputs: -ldl
and it is worked out

Related

undefined reference to `wcstof'

I have a simple program that compiles fine but fails to link due to my passing a wide string to std::stof. If I pass a standard non-wide string to stof, the program compiles and links with no issues.
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
using namespace std;
float f = stof(wstring(L"53.42"));
cout << f;
return 0;
}
c:/mingw/lib/gcc/mingw32/9.2.0/include/c++/bits/basic_string.h:6636: undefined reference to `wcstof'
I'm compiling with mingw 9.2.0 and have tried linking mingwex manually, but doing so doesn't make a difference. g++ main.cpp -lmingwex
What is the problem?

Detect if I'm building Executable or Dynamic Library?

I have two versions of my app, one works as a dynamic library loaded from a host app and another one works as a standalone executable app.
In my code, there are parts where I need to use different code depending on which version I'm building.
Is there a way to detect if I'm building a dynamic library or a standalone executable app so I can use the same source files for the two versions?
I would appreciate a cross-platform solution.
ADDED:
My dynamic lib or executable code:
#include <iostream>
int main(int argc, const char * argv[])
{
#ifdef IS_DYNAMICLIB
std::cout << "Compiled as a dynamic library!\n";
#else
std::cout << "Compiled as an executable!\n";
#endif
return 0;
}
In the terminal on macOS and I build it using the command:
g++ -dynamiclib -undefined suppress -flat_namespace main.cpp -o Test.dylib
Now, I have Test.dylib
Host app code:
#include <iostream>
#include <dlfcn.h>
int main(int argc, const char * argv[])
{
void* handle;
typedef void (*func_t)();
handle = dlopen("/Path/to/Test.dylib", RTLD_LAZY);
if (!handle) {
printf("failed to open the library\n");
return 0;
}
func_t mainFunc = (func_t) dlsym(handle, "main");
if (!mainFunc) {
printf("failed to find main method\n");
dlclose(handle);
return 0;
}
mainFunc();
return 0;
}
When I build and run this, I get:
Compiled as an executable!
Program ended with exit code: 0
But I would like it to print:
Compiled as a dynamic library!
Program ended with exit code: 0
How can I do this?

undefined reference to `boost::filesystem::path::codecvt() CodeBlocks Windows

I'm trying to use boost on CodeBlocks. I have codeblocks configured with MinGW-w64. I alredy have included the following libraries in the linker settings
libboost_system-mgw73-mt-d-x32-1_66.a
libboost_system-mgw73-mt-x64-1_66.a
libboost_filesystem-mgw73-mt-x64-1_66.dll.a
I also try changing the order...
This is the source code
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
namespace fs = boost::filesystem;
int main(int argc, char* argv[])
{
if( fs::is_directory("."))
cout << "Works!" << endl;
return 0;
}
i have read many "solutions" but they didn't work.
i tried:
changing the #include for this
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
creating a new compiler flag with boths values
-lboost_filesystem-mt
and also
-lboost_filesystem
but i always get the same error
...\boost_1_66_0\boost\filesystem\path.hpp|981|undefined reference to `boost::filesystem::path::codecvt()'|

Function Not Found in Custom Library

I have a custom-defined library (and corresponding cpp file) included by a test file. When I try to call the function in the test file, it gives me the error, "Undefined reference to < function name>." I'm not very experienced with putting stuff in library files, so any help is appreciated.
input.h
#ifndef LOC_H
#define LOC_H
#include<vector>
struct loc{
int room, row, col;
char c;
bool stacked;
//loc *north, *east, *south, *west;
};
#endif
void Input(std::vector<std::vector<std::vector<loc> > > &b, loc & start);
input.cpp
#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<getopt.h>
#include "input.h"
using namespace std;
void Input(vector<vector<vector<loc> > > &b, loc & start) {
//Do stuff
}
test.cpp
#include<iostream>
#include "input.h"
#include<vector>
using namespace std;
int main(int argc, char* argv[]) {
vector<vector<vector<loc> > > building;
loc start = {0, 0, 0, '.', false};
Input(building, start);
}
There is not library involved at all. You just need to link the object files of all source files when you are linking. The easiest way is to compile it from source:
g++ -o test test.cpp input.cpp
When you have a larger project you might want to compile separately, controlled by a makefile or script.
g++ -c test.cpp
g++ -c input.cpp
g++ -o test test.o input.o
This looks a bit clumsy, but shows what is done behind the scenes.

Dynamic library using static library in c++ name mangling error

I am trying to create a dynamic(.so) wrapper library along mongoDB c++ driver. There is no problem with the compilation but when I test it in a C++ sample program i get the error
undefined symbol: _ZN5mongo18DBClientConnection15_numConne
which i assume has something to do with name mangling issues.
I compiled the library as
g++ -fPIC -shared mongoquery.cpp -I/pathto/mongodriver -lmongoclient -lboost_thread-mt -lboost_filesystem -lboost_program_options -o libmongoquery.so
Here's the program I am using for testing:
#include <iostream>
#include <dlfcn.h>
#include "mongoquery.hpp"
using namespace std;
int main()
{
void *lib_handle;
int (*fn)(int *,string);
lib_handle=dlopen("./libmongoquery.so",RTLD_NOW);
if(!lib_handle)
{
cerr<<"Error"<<dlerror();
return 1;
}
fn=(int (*)(int *,string))dlsym(lib_handle,"count_query");
string q="{}";
int n;
(*fn)(&n,q);
cout<<n;
dlclose(lib_handle);
return 0;
}
the header mongoquery.hpp contains
#include <iostream>
#include <client/dbclient.h>
#define HOST "localhost"
#define COLLECTION "test.rules"
using namespace mongo;
using namespace std;
class mongoquery
{
private:
string q;
mongo::DBClientConnection c;
public:
mongoquery(string);
int result_count();
};
int count_query(int *,string);
Thanks
The answer can be followed from this question
Dynamic library uses statics libraries, undefined symbols appears
Added for achival purpose