How do I use a shared dynamic library in c++ project? - c++

I am running 64bit Linux and using Netbeans 8.0 IDE. I simply created new C++ project, so far only containing some HelloWorld code. I want to use a third party library for logging. I chose log4cpp (since I have experience with Java and it is supposed to have similar interface).
I installed liblog4cpp from the repositories, and can see it in /usr/lib, to be clear:
$ locate log4cpp
/usr/lib/liblog4cpp.so
/usr/lib/liblog4cpp.so.5
/usr/lib/liblog4cpp.so.5.0.5
/usr/lib/x86_64-linux-gnu/liblog4cpp.so
/usr/lib/x86_64-linux-gnu/liblog4cpp.so.5
/usr/share/doc/liblog4cpp5
/usr/share/doc/liblog4cpp5/AUTHORS
/usr/share/doc/liblog4cpp5/NEWS.gz
/usr/share/doc/liblog4cpp5/README
/usr/share/doc/liblog4cpp5/THANKS
/usr/share/doc/liblog4cpp5/TODO
/usr/share/doc/liblog4cpp5/changelog.Debian.gz
/usr/share/doc/liblog4cpp5/changelog.gz
/usr/share/doc/liblog4cpp5/copyright
/var/cache/apt/archives/liblog4cpp5_1.0-4_amd64.deb
/var/lib/dpkg/info/liblog4cpp5.list
/var/lib/dpkg/info/liblog4cpp5.md5sums
/var/lib/dpkg/info/liblog4cpp5.postinst
/var/lib/dpkg/info/liblog4cpp5.postrm
/var/lib/dpkg/info/liblog4cpp5.shlibs
I am having big trouble on how to #include it in my application. Inspired by example from the documentation I put in my source:
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
I was first using Makefile generated by netbeans, however could not figure out how to configure the project to make it work. All my trials lead to the following error (line 16 being the first #include):
$ make
src/main.cpp:16:31: fatal error: log4cpp/Category.hh: No such file or directory
compilation terminated.
make: *** [build/NetworkConfiguration.o] Error 1
I also tried a custom Makefile, giving g++ argument -llog4cpp, but I always get the same error. I can provide more info/code if it will be helpful. The solution does not need to depend on the IDE, I feel like I just misunderstood how linking works, so feel free to educate me on how to achieve what I want from console with custom Makefile for example.

Since cup still has not posted an answer, I am paraphrasing his comment as an accepted answer.
Install liblog4cpp-dev. The difference is that liblog4cpp only contains the libraries while liblog4cpp-dev is the development kit which includes the headers.

Related

How do I install libraries for <stropts.h>?

I am running Kali-Linux (debian+gnome). When I compile I get compilation error:
cc -Wall -g -c -o frag.o frag.c
frag.c:7:10: fatal error: stropts.h: No such file or directory
7 | #include <stropts.h>
| ^~~~~~~~~~~
compilation terminated.
make: *** [<builtin>: frag.o] Error 1
Can anyone tell me what am I missing and what should I install?
I tried installing glibc-sources but still it didn't work out.
As mentioned by the other answer, this library is not used on Linux. Since this came up when trying to compile an application on Linux, it's possible an #if was not set correctly.
As a workaround, look at the source code to see what the #if surrounding the #include is, and set that to false when compiling.
For example, if the code looked like:
#if HAVE_STROPTS_H
#include <stropts.h>
#endif
And if you are using cmake or gcc, run them with -DHAVE_STROPTS_H=0.
stropts.h is part of the Posix STREAMS extension, which Linux never supported. (Since 2008, it has also been marked as obsolescent by Posix, so it may be removed in some future standard revision.)
There have been third-party implementations of STREAMS for Linux, but I don't kniw if any of them are still supported. One which is used by Linux-based telephony apps is contained in openss7.
Another reason not yet mentioned might be the confusion manual introduces. If you need to use ioctl() call, the quickest way to find out the header to include might be pulling up a man ioctl. Well, it turns out there are 2 different manual pages for ioctl(), and having both on the system will result in this misleading and wrong suggestion (accessible directly as man 3 ioctl) of including the stropts.h.
More likely you actually need a sys/ioctl.h, described in man 2 ioctl:
#include <sys/ioctl.h>

How to integrate sqlite3 C++ interface

I'm trying to use SQLite3 for database managing in my little project on Windows, I just made some C++ code to generate my database but I can't link my code with the "sqlite3.h" file, no matter if I use
#include <sqlite3.h>
compiling with
g++ DBconfig.cpp -l sqlite3
(in that case the compiling error is "fatal error: sqlite.h: no such file or directory").
I can't either use
#include "path/sqlite3.h"
Tried compiling in many ways, results in tons of declared but not defined things like the function "sqlite_exec()".
Everywhere people fix this easily by using a linux "sudo" command or downloading and compiling sqlite3 libraries, I tried that, nothing removes these errors. I have SQLite 3 installed, along with its interface (https://www.codeproject.com/Articles/6343/CppSQLite-C-Wrapper-for-SQLite) files that I ran with "lib /def:" command.
Internet tutorials say just installing sqlite3, including the first way I did and compiling with "-l sqlite3" should work.

getopt.h missing in aix 7.1

I am having a hard time figuring out how to add getopt.h in my AIX 7.1. I am using the getopt_long function in my code, which I know is in getopt.h instead of unistd.h (which contains getopt()).
This code is not compiling in AIX:
fatal error: getopt.h: No such file or directory
#include <getopt.h>
^
compilation terminated.
Here are the gcc packages installed:
gcc-4.8.3-1.aix7.1.ppc.rpm
gcc-c++-4.8.3-1.aix7.1.ppc.rpm
libgcc-4.8.3-1.aix7.1.ppc.rpm
gcc-cpp-4.8.3-1.aix7.1.ppc.rpm
I have gone through these links, but they haven't helped much:
http://www.ibm.com/developerworks/aix/library/au-unix-getopt.html#sthash.Loz3H0AH.dpuf
http://www.linuxmisc.com/8-freebsd/eb9bdab1cf6044d5.htm
Also a lot of other web searches.
Am I missing some rpms, or some environment variables?
getopt_long is GNU-only, but you can take free implementation (for example, this claims that it is of BSD license, if I understand it right) or write your own implementation (it's not so hard) and use it in your code.

Xcode noob: Don't understand compiler error message

I'm fairly new to Xcode and have recently gotten an error message that is probably clear to anyone who knows what they're doing, but I am unsure how to react to this one.
I have a project that is mostly C++, with just a few C files in it. Everything was fine until I tried to add some code to find the computer's MAC address. Apple provides a project example (GetPrimaryMACAddress), and I downloaded, built, and tested that. It works just fine.
After that, I simply copied the C source from the Apple example project and included it into my code project. Then I started getting this error message:
I have tried including the IOKit framework explicitly via the linker; no help. I tried adding more #include statement to the Apple example code; no help. I think that I do not understand what the compiler message is telling me regarding importing from module 'Darwin.MacTypes'.
Clicking on the error message took me to a line in usr/include/MacTypes.h:
I'm not clear on how to Import Darwin.MacTypes. I don't really understand how the source code for GetMACAddress could compile so effortlessly in one project and not another. I didn't rearrange nor add nor delete any of the #include statements in the Apple-supplied C file.
I'll bet there is a simple answer that I am just not seeing. How should I react to this error message?
I had a similar problem with types like UInt16 and UInt32. I just included the MacTypes with the following include statement:
#include <MacTypes.h>
This solved all my type-problems for my example c-file.
Let me guess, when you want to compile C++ source, you should create a C++ source file
(On the toolbar: File > New > File.. > Source > C++ File)

Basic example code compiling help required - I can't get any SDK examples to work

I'm very new to C++; I've worked with several SDKs now on various applications and every time come across the problem that I can't get the 'example code' to compile. This is a very broad question basically regarding ANY example code that is given over the net - what is the standard procedure to make things compile? I know how to compile code that I've written myself but when given a large project containing several CPP and H files, what do I start with? My first port of call, to open 'main.cpp' in Dev-C++ and hit the 'compile' button generally throws up errors about header files not being available and so on.
I won't give a specific example as this has happened several times. I feel as someone getting to grips with C++ that I would learn a lot quicker if I could start with code that works and tweak it myself rather than having to fumble around building things up piece by piece.
The most recent example is a set of example code provided by a company which 10 files:
-Arial.ttf
-demo_resources.rc
-icon.ico
-main.c
-simple.dsp
-simple.dsw
-simple.exe
-simple.h
-trial.c
-trials.c
Running the .exe file works absolutely fine; however if I open main.c and press compile, I receive many error messages. As an example, the first two lines of code in main.c are:
#include "simple.h"
#include <sdl_text_support.h>
This alone spews the error messages:
1: expected unqualified-id before "public"
1: expected `,' or `;' before "public"
2: In file included from trial.c
Clearly I am doing something very wrong as this code must have compiled for someone else in the past to have generated the .exe file. Again this is not an isolated issue, I have this problem all the time.
Since Dev-C++ is perfectly equipped to deal with plain old C files, I can't see that that is the issue. Secondly, simple.h is definitely included in the correct directory. The second include though, sdl_text_support.h is obviously not in my file list above. I have searched the rest of the SDK and found the file lurking elsewhere. How do I explicitly reference the location of the header file using Dev-C++?
Any general tutorial to how to compile pre-made projects or help of any kind would be greatly appreciated.
I like this page:
http://www.cprogramming.com/tutorial.html
I am not familiar with DevC++, but you cannot assume that if you can open main.c and press a button, then everything will work out. No build system is that smart.
If you write your own code (and you understand compiling and linking) then you can keep your files in order and know exactly how to build everything; someone else's codebase may come with a makefile or some other guide to it's organization, but you'll have to learn how to use a good build system, and the one you're using sounds inadequate.
open the project by simple.dsw instead of main.cpp and it should work .