Reusing Slightly Altered Makefile for Slightly Altered Program Gives Error - c++

I made a copy of a makefile that worked for program A for a new program called program B. To keep things simple program B has all of the same include directives as program A. The only changes made to the new makefile are the obvious changes to the list of object files and the name of the created executable. I can also be sure that the compilation error is not caused by anything in main() or any of the functions of program B. Yet somehow I have an error when I use the make command that goes:
/usr/local/triclops/lib/libtriclops.a(triclops.o): In function `triclopsGetDynamicLibPath':
triclops.cpp:(.text+0x198): undefined reference to `dladdr'
In my makeflie I have the following relevant lines:
CPPFLAGS+=-I/usr/local/triclops/include
LDLIBS+=-L/usr/local/triclops/lib
LDLIBS+=-lpgrlibdcstereo -ltriclops -lpnmutils
I appreciate you help, so thanks in advance. I do not know a lot about makeflies, so I am just trying to reuse the code effectively.
EDIT
Both program A and program B have the same include directives
#include "stereoCamera.h"
#include "Aria.h"
#include <iostream>
#include <cstdio>
#include <cv.h>
#include <highgui.h>
#include <cmath>
#include <vector>
#include <opencv2/highgui/highgui.hpp>
Program B can be thought of essentially as this plus an empty int main(){ return0;} while program A does contain much code and has been working for quite some time now.

You could try linking against libdl with -ldl added to your second LDLIBS+= line. You may also need to add the path of libdl.so typically /usr/lib/ to the first LDLIBS+= line.
I cannot answer why program A compiles with the 'same' makefile while program B fails without looking at the programs or the makefiles though.

Related

Why do R packages using C++ use #include <R.h>, #include <Rdefines.h> and #include <Rmath.h>?

I am working on a massive R package with a lot of old C++ files in it. Only one function is called by .Call in R.
At the beginning there is a lot a #include call, that I'm removing by commenting and compiling, but one thing I don't understand is the difference between:
#include <R.h>
#include <Rdefines.h>
#include <Rmath.h>
I want to clean this part of every files to remove useless libraries, but in most examples I see, there is both R.h and Rdefines.h, although commenting R.h return no warnings during the compilation. Is it usefull ?
PS : The final objective is to only use Rcpp, but for now I first need to understand and clean the project.

WlanGetNetworkBssList()-like commands do not compile with g++

I'm trying to compile a program that prints all available wifi access points including their BSSID.
The example of WlanGetAvailableNetworkList() function on MSDN website compiled just fine when removing the flag part with WLAN_AVAILABLE_NETWORK_CONNECTED.
To print BSSIDs as well I edited that code. I added the following functions and structs:
WlanGetNetworkBssList()
WlanScan()
PWLAN_BSS_LIST
PWLAN_CONNECTION_ATTRIBUTES
PDOT11_MAC_ADDRESS.
All of them except PDOT11_MAC_ADDRESS are marked as 'not declared in this scope'.
My compile command is:
g++ -o WlanAP2 WlanAP2.cpp -lwlanapi -lole32
(The same one worked for the MSDN example.)
My include order is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wlanapi.h>
#include <objbase.h>
What am I missing?
Is there another library to be included?
Why do especially these commands not compile?
Thanks in advance!

c++: How to remove libstdc++.so.6 dependencies

I have 2 program I wrote on my windows computer using Visual Studio 2013. They run fine and work perfectly on my computer, but when I brought them over to my school account that is on a Linux machine, a problem arose. They compile and 1 ran, but the other did not. The one that did not run gave me an error:
.../lib/compat/libstdc++.so.6: version CXXABI_1.3.2 required by...
I have been doing research and I can't seem to find out what in my program would be using libstdc++.so.6, I'm not even really sure what it is or does. Since I am on a student account I can't go installing it using sudo, and it is a homework so I can't submit it using my own libraries.
Any Idea on what my program might be using that would require libstdc++.so.6?
I have 3 files: main.cpp, LinkedList.cpp and LinkedList.h.
I think it might be in main.cpp because I think it stems from a library I am including and main.cpp is the only one that uses outside libraries. Here is the list of libraries it uses:
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <ctype.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <bitset>
#include <algorithm>
#include "LinkedList.h"
Thanks in advance!
You are trying to run a program linked against one version of the libraries under another set. That should work fine as long as the library versions aren't too far apart. In your case, the difference between libraries is just too large.
GCC (C++ in particular) has changed quite a bit lately, some programs that used to compile and run fine now blow up or don't compile at all (due to language changes, compiler bugs accepting broken code, ...), and the library ABI has also changed. Your best bet is to carry source code around, and make sure you got compatible language versions on both ends. If that is inconvenient, a solution is to make sure you have the same compiler (and other environment) at both places. The easiest way to get this is to install the same distribution and version.
First you can't remove the dependencies of libstdc++.so.6, because it's a standard C++ library.
To solve your problem you have to check whether your libstdc++.so have the right version
strings /usr/lib64/libstdc++.so.6|grep GXXABI_1.3.1
if there have no matching version, you will have 2 methods like these:
update your gcc on your school's linux OS
yum intsall gcc
download a matching libstdc++.so from this website:
download gcc || download matching libstdc++
then replace the libstdc++.so to /usr/lib64/libstdc++.so.6.*
SOLUTION
I went through a few steps to find my solution. Originally I could compile my program but could not run it.
1) My first step to solve the issue was to change my method of compiling. Originally I compiled my program with the following: g++ main.cpp LinkedList.cpp -o output. I changed it to: g++ -static main.cpp LinkedList.cpp -o output which allowed me to compile and run. This worked but static is a method to dynamically link libraries. This prevents linking with the shared libraries. This is not a good solution because it takes a lot longer and increases the file size of the executable, so I wanted to improve.
2) The second thing I did was remove using namespace std. Yes, I cheated and used it. So I went through my program and added std:: to the appropriate places.
3) The last thing I did was clean up my code. I was using a lot of libraries because my program was a large and complicated program. I was using all of the libraries I had listed in my original post. I went through my code and anywhere I was using a function from a library I would try and write my own code that would do the same thing which would result in my program not depending on those libraries. I was able to replicate a decent amount of these dependent foreign functions with my own which added lot of code, but it allowing me to remove some of these includes. My list of includes is now:
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include "LinkedList.h"
#include <math.h>
I am not sure exactly which step resolved my issue, but now I can compile with my preferred method, g++ main.cpp LinkedList.cpp -o output, and my program runs fine.
I hope this helps someone.

mpf_set_d causes an illegal instruction in c++

I'm using GMP and getting an illegal instruction that I have found to be caused by mpf_set_d. I'm programming in Netbeans with cygwin in c++.
Thanks in advance!
Edit:
I have a folder containing the following files:
.dep.inc, cyggcc_s-seh-1.dll, cyggmp-10.dll, cyggmpxx-4.dll, cygstdc++-6.dll, cygwin1.dll, my executable, gmp.h, gmpxx.h, libgmp.a, libgmp.la, libgmp.lai, libgmp.lai, libgmp.libcmd, libgmpxx.a, libgmpxx.la, libgmpxx.lai, main.cpp, main.o, main.o.d, Makefile, text file needed for program and folders needed for program.
I have tried many things, one of which was to add an executable to this folder that ran the following code:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "gmpxx.h"
#include <stdarg.h>
#include <string>
using namespace std;
int main()
{
mpf_t a;
mpf_init(a);
mpf_set_d(a,3.1415926535);
cout << "works" << endl;
}
Running this on my own computer with GMP installed gives me 'works' after which it closes, on another computer that does not have GMP installed it throws an error and closes. The error is an exception: status_illegal_instruction.
I can't give you my code but I can post snippets. Please tell me if more information is necessary!
Thanks again.
Edit 2:
The same counts for mpf_set_str as well as the c++ wrapper.

Weird 'undefined reference' error

I have run into a peculiar error in a C++ project at work.
I have found a fix, but I am now really satisfied, as I would like to understand what actually causes the error.
When building this snippet of code:
#include <iostream>
#include "snmp/snmp/SW_SNMP_Values.hpp"
#include "snmp/agent/SW_Agent.hpp"
#include "snmp/agent/SW_Agent_PP.hpp"
int main()
{
//SW_Agent_PP agent;
return 0;
}
Notice that SW_Agent_PP is COMMENTED OUT!! When building this, I get a ton of undefined reference errors, for classes that are in use by the SW_Agent_PP object.
The FIX is to ACTUALLY CREATE THE OBJECT! so if I do this:
#include <iostream>
#include "snmp/snmp/SW_SNMP_Values.hpp"
#include "snmp/agent/SW_Agent.hpp"
#include "snmp/agent/SW_Agent_PP.hpp"
int main()
{
SW_Agent_PP agent;
return 0;
}
everything works fine and dandy.
How can I get linker errors for NOT using something? I would like to hear if anyone have run into similar experiences before, and if they found what caused it.
I am sorry, but I cannot release more code as it is company property.
Many thanks in advance!
Linkers are complicated and this behaviour is by no means unusual. Here's one possible explanation:
You are linking with a static library libfoo.a.
libfoo.a contains foo.o that contains SW_Agent_PP::SW_Agent_PP() and a bunch of other functions.
Another library libbar.a, listed after libfoo.a in the link line, uses a bunch of other functions from libfoo.a.
The linker processes static libraries in order and never goes back. Therefore the references in libbar.a can be only satisfied if corresponding object was pulled from libfoo.a by main().
The solution is to reorder the libraries in the link line.
There are other possible explanations. It's hard to tell without seeing actual code.