Compiling and Linking Leda 6.3 - c++

I am new to LEDA and I am working on LEDA6.3 Free Edition on OpenSuse 12.1.
As a start I tried to write a simple code "Hello LEDA world" as in the manual http://www.leda-tutorial.org/en/official/ch01s02.html.
#include <LEDA/string.h>
#include <iostream>
using leda::string;
using std::cout;
int main()
{
string msg = "Hello LEDA world!";
cout << msg << "\n";
}
The compilation phase works fine and I generated the .o file.
But they mentioned that this file needs to be linked to the LEDA library and the only library available in the Free edition is the libleda.a and libleda.so
I am trying to link using the following command:
g++ -o welcome welcome.o -L$LEDAROOT -llibleda
What I get is:
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/bin/ld: cannot find -llibleda
I was wondering if someone could help me with how to link my file with the leda library ? so I can get it to run.
Thanks all.

It is unlikely that the library file is called liblibleda.a or liblibleda.so: when the linker sees a an option of the form -l<name> it will search for a library file named lib<name>.so (for shared libraries) or lib<name>.a (for static libraries). You probably just want to use the option -lleda (unless the LEDA developers did something clever and called the library something like libeda.a so you'd use -leda).
Whether this works will also depend on the compiler options being consistent between the installation and your build.

Related

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

Linker error when linking C++ code with USB driver (ftdi)

I am on a Mac OSX and I am trying to start using the C ftdi drivers I installed using brew
brew install libftdi
That installed the library in the directory /usr/local/Cellar/libftdi/1.2 And I have the following C++ code
#include <iostream>
#include <ftdi.h>
using namespace std;
int main() {
struct ftdi_context ftdi;
ftdi_init(&ftdi);
cout << "Hello World" << endl;
return 0;
}
How should I compile this to make it work? I am including the include/ directory so the compiler can find the header file but I do not know which object file should be linked. I am trying to using the following command
g++ -I /usr/local/Cellar/libftdi/1.2/include/libftdi1/ /usr/local/Cellar/libftdi/1.2/lib/libftdi1.a test.cpp
But I am getting bad linker errors even though I have included the .a file. The lib/ directory has the following contents
cmake
libftdi1.2.2.0.dylib
libftdi1.2.dylib -> libftdi1.2.2.0.dylib
libftdi1.a
libftdi1.dylib -> libftdi1.2.dylib
pkgconfig
Is there something else I should try and link it to? Even if you are not aware of this particular driver is there something in general that I should be doing for drivers like this that I have not done?
Thanks for the help!

Using Boost library in XCode

not particularly good with this kind of stuff I have managed to install the Boost C++ library and this basic program works with the g++ compiler and linking to the right .dylib files.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost;
int main()
{
filesystem::path path1("/Users/username/Documents/testfile.txt");
filesystem::remove(path1);
std::cout << "It worked" << std::endl;
return 0;
}
Now I would like to do the same, just using Xcode to build and run the program, but it doesn't work. I have done everything in this tutorial:
http://buddypanda.com/?p=10
i.e. include header and library search paths, as well as linked the .dylib files of filesystem and system under the 'Build Phases' tab.
The Build succeeds but then the program crashed with an error of EXC_BAD_ACCESS.
I can maybe provide more specifics on what goes wrong if you tell me where to look.
Thanks!
EDIT1: I am using g++-4.9 and Boost-1.58 and Xcode 6.4

Simple C++ program using pqxx (postgres)

I'm trying a very basic C++ program using Code::Blocks. I'm on Ubuntu 12.04 and installed pqxx from the software manager. Here's the code.
#include <pqxx/pqxx>
#include <iostream>
using namespace std;
int main()
{
pqxx::connection MyConn ("dbname=dbESM user=postgres");
cout << "Hello world!" << endl;
return 0;
}
But I get the following error on hitting F9 to compile and run:
/usr/include/pqxx/connection.hxx|87|undefined reference to
`pqxx::connectionpolicy::connectionpolicy(std::basic_string, std::allocator > const&)'
The above message is from the file connection.hxx and the line highlighted is this:
explicit connect_direct(const PGSTD::string &opts) : connectionpolicy(opts) {}
The connection.hxx file is not mine - I think it's part of pqxx.
I'm pretty new to this platform so I'm avoiding the terminal to compile code. Any help would be greatly appreciated.
You need to add the reference to the libpqxx library to the project.
Inside Code::blocks, when the project is open, locate Project in the menus, then follow Build options, then open the tab called Linker settings, then hit Add, then enter pqxx.
If you were using the libpq C library instead, the procedure would be identical except the name would be pq.
You need to link against the according library, just #including the header files isn't enough. If available, you could use pkg-config to determine the according libraries. Further, what IDE are you using? Without that, the "on hitting F9" reference is useless. Also, compiling this on the commandline might even be easier, since it is clearer what exactly is happening.

Linking jsoncpp (libjson)

I'm trying to link jsoncpp (lib_json) with a c++ project using cmake. It works perfectly fine on one computer, but on another one (with pretty much the same config) i get an error when i execute my app :
dyld: Library not loaded: buildscons/linux-gcc-4.2.1/src/lib_json/libjson_linux-gcc-4.2.1_libmt.dylib
Referenced from: path to executable
Reason: image not found
Any idea what might be causing this ? I don't even understand why it tries to look # buildscons/linux-gcc-4.2.1/src/lib_json/libjson_linux-gcc-4.2.1_libmt.dylib since i put jsoncpp in usr/lib/ and changed the name to libjsoncpp and cmake find the correct path/library.
I also built jsoncpp the exact same way on both computers.
I had the same problem. If you run tool -L libjson_linux-gcc-4.2.1_libmt.dylib you can see some weird relative address to your libjson.... I guess if you replicated this directory structure it would work but that's a bad solution.
What I did instead is that I used .a (libjson_linux-gcc-4.2.1_libmt.a) and linked it staticaly with my binary. In XCode simply under Build Settings -> Linking -> Other Linker Flags I added absolute path to my .a. For me it was /Users/martin/Downloads/jsoncpp-src-0.5.0/libs/linux-gcc-4.2.1/libjson_linux-gcc-4.2.1_libmt.a and that's all.
Of course, I don't know your use case, maybe you really need to link it dynamically.
EDIT: I see now, you mean libjson, and not libjsoncpp (they're different!)
In your titel you talk about jsoncpp, and that's what this answer is for.
But maybe it's useful for people who got confused by the titel too.
You can 'amalgamate' jsoncpp.
From jsoncpp source dir run python amalgamate.py which creates:
dist/jsoncpp.cpp
dist/json/json.h
dist/json/json-forwards.h
Now you have to compile jsoncpp.cpp once and just link against the resulting jsoncpp.o:
g++ -o jsoncpp.o -c jsoncpp.cpp (only once)
g++ -o executable jsoncpp.o main.cpp (every time)
If you get errors, you might have to #define JSON_IS_AMALGAMATION before including json/json.h, but ...
... I tried it and it worked for me. (without #define JSON_IS_AMALGAMATION, that is)
Used code:
#include "json/json.h"
#include "json/json-forwards.h"
int main(int argc, char *argv[])
{
Json::Reader reader;
Json::Value value;
if (!reader.parse("{\"hello\":\"world\"}", value, false))
{
std::cerr << "ERROR: Couldn't parse Json: " << reader.getFormattedErrorMessages() << std::endl;
return -1;
}
std::cout << value.toStyledString() << std::endl;
return 0;
}