Having trouble using libstatgrab -- I receive the following error at compile time:
"libstatgrabTest.cpp:16: undefined reference to sg_get_process_stats"
I'm guessing it is because I need to include it's .so files at the linking state -- although I'm not sure. As you can see below, I am currently including the statgrab.h header file. I performed configured && make && make install for the library.
If I search for libstatgrab*, I come across the following:
./usr/local/lib/libstatgrab.so.6.2.3
./usr/local/lib/libstatgrab.la
./usr/local/lib/libstatgrab.so.6
./usr/local/lib/libstatgrab.a
./usr/local/lib/libstatgrab.so.6.2.2
./usr/local/lib/libstatgrab.so
./usr/local/lib/pkgconfig/libstatgrab.pc
Another search for statgrab* returns the following (relevant items only):
./usr/local/bin/statgrab
./usr/local/include/statgrab_deprecated.h
./usr/local/include/statgrab.h
At compilation, I run: g++ -g -c libstatgrabTest.cpp
At linking, I run: g++ -L/usr/local/lib libstatgrab.o -o libstatgrabTest
Any idea what I am doing wrong? The code within libstagrabTest.cpp is shown below:
// external libraries
#include <statgrab.h> // libstatgrab (http://www.i-scream.org/libstatgrab/)
// namespace
using namespace std;
int main(void) {
// try to initalize libstatgrab
int * entries;
sg_process_stats * systemStats = sg_get_process_stats(entries);
// return
return 0;
}
g++ -L/usr/local/lib libstatgrab.o -o libstatgrabTest
should be (tested with your code snipper on Ubuntu Natty):
g++ -L/usr/local/lib -o libstatgrabTest -lstatgrab
or, to link statically:
g++ -L/usr/local/lib -o libstatgrabTest /usr/lib/libstatgrab.a
Tested both on my box, with your exact source.
Related
I am having trouble installing a dependency for a program that itself depends on pcre.h. I have this installed to /opt/local/include, but the C compiler does not see it and thus gives me:
error: pcre.h: No such file or directory
I have confirmed this by writing a hello world program that tries to include it:
#include <pcre.h>
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
return 0;
}
This also gives the error unless I specify the path as </opt/local/include/pcre.h>.
I would like the C compiler to find this by default but I do not know where this is configured. Tab completion hasn't revealed any HEADER_PATH environment variables and I cannot find anything like it that isn't specific to XCode. I am, however, using Mac OSX Snow Leopard on the off chance that makes a difference.
Use -I /opt/local/include on the command line or C_INCLUDE_PATH=/opt/local/include in the environment.
Use the pcre-config utility to get the right flags:
$ pcre-config --libs --cflags
-L/opt/local/lib -lpcre
-I/opt/local/include
If you're compiling via the command line,
$ gcc -Wall -g `pcre-config --libs --cflags` main.c
Let me first set the context, it is CERN's ROOT and CINT and ACLiC etc.
Suppose I have a main macro named macro.cpp and two headers h1.cpp (contains the definition of a function) and h1.h containing the declaration of the function defined in h1.cpp similarly I have h2.cpp and h2.h. The main program macro.cpp calls those functions inside h1 and h2. I was successful compiling the source files using:
root [0] .L h1.cpp+
root [1] .L h2.cpp+
root [2] .L macro.cpp+
which generated three .so files macro_cpp.so, h1_cpp.so and h2_cpp.so. I want to know what to do with them ? How do I link them so that I have something like a "macro.out" or something like that (a single executable file of some kind) which I can execute (although I don't know how !) and achieve whatever I wished to achieve with the macro.
Note: If I just load all the files using .L file_name.cpp etc and just execute the main macro using .x macro.cpp then everything works fine and I have results, but this is not what I want ! I want to compile like we do in usual g++ and by the way in every forum everyone keeps advising on compiling using .L file_name.cpp+ or ++ .. I would really like to know the whole story. Because nobody seems to explain beyond .L file_name.cpp+ .. what next ? What to do with the .so etc.
I am a beginner, I will really appreciate a simple and step by step answer and explanation.
Thanks.
Edit-1: I am working with:
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Edit-2: ROOT related information:
ROOT 5.34/36 (v5-34-36#v5-34-36, dic 07 2016, 23:31:51 on linuxx8664gcc)
CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
If you want to compile and link you can use a standard compiler instead of Cint/Aclic.
For example, assuming you are working on a *nix platform, you can use the example files below:
h1.h
int add_one(int a);
h1.cpp
#include "h1.h"
int add_one(int a)
{
return a+1;
}
h2.h
#include <TLorentzVector.h>
TLorentzVector multiply_by_two(const TLorentzVector v);
h2.cpp
#include "h2.h"
TLorentzVector multiply_by_two(const TLorentzVector v)
{
return 2.0*v;
}
macro.cpp
#include "h1.h"
#include "h2.h"
#include <TLorentzVector.h>
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int a = 0;
TLorentzVector v;
v.SetPtEtaPhiM(1.0, 0.0, 0.0, 0.0);
cout<<"calling add_one on "<<a<<": "<<add_one(a)<<endl;
cout<<"calling multiply_by_two on "<<v.Pt()<<": "<<multiply_by_two(v).Pt()<<endl;
return 0;
}
Then you can compile with
g++ -c -g -Wall `root-config --cflags` h1.cpp
g++ -c -g -Wall `root-config --cflags` h2.cpp
g++ -c -g -Wall `root-config --cflags` macro.cpp
and link with
g++ `root-config --glibs` h1.o h2.o macro.o
The executable will be a.out:
$ ./a.out
calling add_one on 0: 1
calling multiply_by_two on 1: 2
You can put these g++ commands in a script or, when you start having several files and directories, you can write your make file (or cmake). For this last step, see for example the tutorial here
http://www-pnp.physics.ox.ac.uk/~brisbane/Teaching/Makefiles/Tutorial_1_Makefiles_and_ROOT.pdf
Note 1: one advantage of using g++ is that you will get clear error messages when something doesn't compile. The error messages from Cint can
be difficult to understand--although this is very much improved in root 6 with Cling.
Note 2: another advantage of using a standard compiler is that you will be able to easily link your main executable against libraries other than root.
This answer is based mostly on the answer by user2148414, but if one follows the answer will notice that there were some issues with the method of linking the source (*.cpp) files. My answer also addresses another important object called a TApplication that will play a crucial role in such applications involving root libraries. The following linking step:
g++ `root-config --glibs` h1.o h2.o macro.o
will likely show a lot of errors complaining about the root objects like TWhatever (in user2148414's answer TLorentzVector will show problems). In the comments to that answer one can find the discussion on including various physics libraries that can solve the problem but without discussing that (and I am not comfortable either :) ) let me write down the command that solves everthing.
This procedure is a one-liner, that is no need to compile individual files, create *.cpp files and *.h files as discussed in that answer then compile and link and create a single executable named "someExecutable" using:
g++ macro.cpp h1.cpp h2.cpp `root-config --libs --cflags` -o someExecutable
or better (and one should do it)
g++ -Wall -Wextra -Werror -pedantic -std=c++14 macro.cpp h1.cpp h2.cpp `root-config --libs --cflags` -o someExecutable
This will solve my original answer but for completeness I would like to add a few more things.
TApplication
My original motivation was to create an application that talks to "ROOT" but I didn't want to work with the ROOT shell, CINT, ACLiC etc and wanted to work entirely with g++. user2148414's and my answer will solve the part of creating an application but the application will not serve any purpose, it will run, create histograms draw them and do all the stuff but all the canvases will close in the end when the code reaches "return 0;". To keep the canvases open we will need "TApplication". So the consider the main of user2148414's answer, I am going include just two more lines and include two arguments to the main:
macro.cpp
#include "h1.h"
#include "h2.h"
#include <TLorentzVector.h>
#include <iostream>
using std::cout;
using std::endl;
int main(int argc, char* argv[]) //introduced arguments to main
{
// here I introduce TApplication
TApplication* SomeApp = new TApplication("SomeApp",&argc, argv);
int a = 0;
TLorentzVector v;
v.SetPtEtaPhiM(1.0, 0.0, 0.0, 0.0);
cout<<"calling add_one on "<<a<<": "<<add_one(a)<<endl;
cout<<"calling multiply_by_two on "<<v.Pt()<<": "<<multiply_by_two(v).Pt()<<endl;
//and just before returning 0
SomeApp->Run();
return 0;
}
Today, I read the web blog article, How to make executable shared libraries . In this article it states that if one types at a Linux command prompt:
gcc -shared service.c -o libservice.so -Wl,-soname,libservice.so -W1,-e lib_entry
followed by
./libservice.so, then we can directly executer the lib_entry function.
However, when I run a similar g++ command:
g++ -shared one.cpp two.cpp three.cpp -o libservice.so -Wl,-soname,libservice.so -W1,-e lib_entry
where lib_entry is a C function defined in two.cpp I get the warning message:
No entry point lib_entry point found.
How do I fix this warning message so I can directly run the entry point, lib_entry? Should I enclose the implementation of the C function foo with extern "C" linkage to resolve this problem?
This is my answer:
Missing include "bits/c++config.h" when cross compiling 64 bit program on 32 bit in Ubuntu
sudo apt-get install gcc-4.9-multilib g++-4.9-multilib
Please disregard the previous answer. The answer below was tested successfully. Thank you for your patience.
Step 1
#include <stdio.h>
#include <unistd.h>
#ifdef __LP64__
const char service_interp[] __attribute__((section(".interp"))) = "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2";
#else
const char service_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
#endif
void lib_service(void)
{
printf("This is a service of the shared library\n");
} // lib_service
void lib_entry(void)
{
printf("Entry point of the service library\n");
_exit(0);
}
Step 2.
vendor#clickit:~/Downloads/DataServerLib$ g++ -shared -fPIC -DLINUX -Wl,-soname,libdataserver.so -efunc -I /home/vendor/Downloads/waitForMultipleObjects -I /home/vendor/development/Test/Include DataServer.cpp DataServerLib.cpp DataTransferClient.cpp CWinEventHandle.cpp WinEvent.cpp -o libDataServer.so -lpthread -lrt
maryych#uwash.edu:~/Downloads/DataServerLib$ chmod 777 libDataServer.so
maryych#uwash.edu:~/Downloads/DataServerLib$ ./libDataServer.so
Inside entry point tester 1
AddUser
I am trying to use selective parts of the Festival code (written in C++) and trying to use them in my own C++ programs. Note that this question is not about using the Festival API but about functions within Festival that can be used directly.
The program I wrote takes in a C++ style string and tries to initialize an object of type EST_String (an internal implementation of the String class in Festival). I then try to print the object.
The code I have:
/*EST_String is a string implementation for the festival project.
* This program simply takes in a C++-style string and returns an EST_String.
* This program is being used to test for makefiles etc.
*/
#include <iostream>
#include <EST_String.h>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[]) {
if(argc != 2) {
cout << "Correct usage: <binary> <string>" << endl;
exit(5);
}
string word(argv[1]);
EST_String e(word.c_str()); //init EST_String.
cout << "C++ String = " << word << endl;
cout << "EST_String = ";
cout << e;
return 0;
}
I am trying to compile it (from the command line directly (no makefile at present)) like so:
g++ -I../../speech_tools/include -I../../speech_tools/base_class/string -L../../speech_tools/lib/ -lestbase -lncurses -lasound -leststring -lestools usingESTString.C -o usingESTString
The error I get is:
/tmp/cczyGTfm.o: In function `main':
usingESTString.C:(.text+0x91): undefined reference to `EST_String::EST_String(char const*)'
/tmp/cczyGTfm.o: In function `EST_Chunk::operator--()':
usingESTString.C:(.text._ZN9EST_ChunkmmEv[EST_Chunk::operator--()]+0x3e): undefined reference to `EST_Chunk::~EST_Chunk()'
usingESTString.C:(.text._ZN9EST_ChunkmmEv[EST_Chunk::operator--()]+0x49): undefined reference to `EST_Chunk::operator delete(void*)'
collect2: ld returned 1 exit status
How can I get the code to compile properly? Where am I going wrong?
Try putting the libraries you link with last on the line.
The linker often resolves references kind of "backwards", meaning that the order of files presented on the command line is important: It wants files containing references first, then the libraries containing those references.
Try adding this to end of your g++ link command: -I/usr/include/festival
-I/usr/include/speech_tools -I/usr/include/boost -lFestival -lestools
-lestbase -leststring
Making sure that the festival and speech_tools headers directory lives at: /usr/include
cd /usr/include
ls festival
ls speech_tools
I am trying to rebuild cogita with festival support, and my program linked successfully after compiling the object files using this line
g++ -Wall -fPIC -Wno-variadic-macros -fopenmp -std=gnu++0x -O2 -g -fstack-protector cogitaconfig.o go-irc.o irc.o whirr-sockets.o -o cogIRCProgram
-rdynamic /usr/local/lib/libcogutil.so -Wl,-rpath,/usr/local/lib
-I/usr/include/festival -I/usr/include/speech_tools -I/usr/include/boost
-lFestival -lestools -lestbase -leststring
I've been trying to link to festival's API too, and the Makefile I wrote executes the following link command
g++ -lpthread build/fetch/festival/src/lib/libFestival.a build/fetch/speech_tools/lib/libestools.a build/fetch/speech_tools/lib/libestbase.a build/fetch/speech_tools/lib/libeststring.a -lcurses -ldl -lm -lstdc++ build/test/speaker.o build/common/message-queue.o build/speaker/speaker-public.o build/fetch/festival/src/lib/libFestival.a -o build/bin/speaker-test
and I get a huge (25k lines) linker error full of undefined references (a part of which is here: http://pastebin.com/PCyV8xAH). I can assert that the *.a files exist (though I'm not sure if they've been built correctly or not). I compile speech_tools with make -j7 and festival with make.
Any suggestions?
I'm running Debian wheezy.
I am trying to run PostgreSQL on my mac. PostgreQL itself works fine and I can create database and table and stuff but when I try to connect to PostgreSQL using C++ with something like:
#include <stdio.h>
#include </Library/PostgreSQL/8.4/include/libpq-fe.h>
#include <string>
int main() {
PGconn *conn;
PGresult *res;
int rec_count;
conn = PQconnectdb("dbname=ljdata host=localhost user=dataman);
if (PQstatus(conn) == CONNECTION_BAD) {
puts("We were unable to connect to the database");
exit(0);
}
res = PQexec(conn, "update people set phonenumber=\'5055559999\' where id=3");
and compile with something like:
g++ -lpq db.cpp -o db
I get the error
ld: library not found for -lpq
and if I compile without lpq, I get
Undefined symbols:
"_PQclear", referenced from:
_main in ccpjNCAU.o
_main in ccpjNCAU.o"
I have already included the libpq-fe.h, shouldn't it work? Does anybody know what went wrong?
g++ can't find the pq library. You have to specify where to look for it, with a capital -L:
g++ -L/path/to/pq/lib -lpq db.cpp -o db
where pq is /path/to/pq/lib/libpq.a (or whatever the extension is)
Here's what you probably want to do:
change the include line to not have the path.
#include "libpq-fe.h"
Add the include path to the commandline
g++ -I/Library/PostgreSQL/8.4/include db.cpp
Build intermediary object files
g++ -I/Library/PostgreSQL/8.4/include db.cpp -c -o db.o
Link it together as a separate step
g++ -L/Library/PostgreSQL/8.4/lib db.o -lpq
Build with debug info using -g
Put it all together, for two separate build steps: compile and link:
g++ -I/Library/PostgreSQL/8.4/include db.cpp -c -g -o db.o
g++ -L/Library/PostgreSQL/8.4/lib db.o -lpq -o db
libpq-fe.h is a user library, not a system library, and therefore you should use "..." instead of <...>, like this:
#include "/Library/PostgreSQL/8.4/include/libpq-fe.h"
Take a look at this link. And make sure libpq-fe.h can actually be found by your compiler.
Had the same problem, You need to add the path of the library to /etc/ld.so.conf, do it and you'll see.
Good luck