C++ Compiling with Python.h Undefined Symbols - c++

So, I've been trying to start using Python.h for a little project I want to work on that seems pretty /simple/. But before I start I want to try to learn how to use Python.h.
So I found this little example online.
#include "Python/Python.h"
int main(int argc, char** argv)
{
Py_Initialize();
PyRun_SimpleString("print 'Test'");
PyRun_SimpleString("print str(3 + 5)");
Py_Exit(0);
}
Seems pretty straight forward. When i first used
gcc test.cpp
to compile, i got some undefined symbols. I quickly found out I should use
-lpython2.7
then I found out I could also use
-L/Library/Frameworks/Python.framework/Versions/2.7/lib/
that didn't work (I made sure that /Library/Frameworks/Python/Versions/2.7/lib/ existed)
I'm stuck, what do I do?
I get
Undefined symbols:
"_Py_Initialize", referenced from:
_main in ccoUOSlc.o
"_PyRun_SimpleStringFlags", referenced from:
_main in ccoUOSlc.o
_main in ccoUOSlc.o
"___gxx_personality_v0", referenced from:
_main in ccoUOSlc.o
CIE in ccoUOSlc.o
"_Py_Exit", referenced from:
_main in ccoUOSlc.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
EDIT:
I just tried using the -Framework argument, and tried adding after the -L the -l python2.7 argument, and I now get
Undefined symbols:
"___gxx_personality_v0", referenced from:
_main in ccfvtJ4j.o
CIE in ccfvtJ4j.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Now what?

If you are using an Python framework installation on OS X as it appears you are based on the paths, you can use the -framework argument to the Apple compiler drivers:
cc test.cpp -framework Python
Alternatively, you can explicitly specify the directory path and library name:
cc test.cpp -L /Library/Frameworks/Python.framework/Versions/2.7/lib/ -l python2.7
Update: With the configuration you report in the comments (Xcode 3.2.6, gcc-4.2), it appears you need to explicitly invoke the c++ variant of gcc. Either:
g++ test.cpp -framework Python
or
c++ test.cpp -framework Python
should work.

Related

Got an 'Undefined Symbols' error when compiling .cpp file with ginac

I'm using gcc(version 11) to compile my .cpp file, and the code is below, which is copied from the official website of ginac.
#include <iostream>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;
int main()
{
symbol x("x"), y("y");
ex poly;
for (int i=0; i<3; ++i)
poly += factorial(i+16)*pow(x,i)*pow(y,2-i);
cout << poly << endl;
return 0;
}
I used the compiling command like this:
gcc-11 -lstdc++ -lginac -lcln hello.cpp -o hello -I /usr/local/include -L /usr/local/lib
After that, I got some errors like this:
Undefined symbols for architecture x86_64:
"__ZN5GiNaC12archive_node6add_exERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_2exE", referenced from:
__ZNK5GiNaC9containerISt6vectorE7archiveERNS_12archive_nodeE in ccEkVJyo.o
"__ZN5GiNaC5basic12read_archiveERKNS_12archive_nodeERNS_9containerINSt7__cxx114listEEE", referenced from:
__ZN5GiNaC9containerISt6vectorE12read_archiveERKNS_12archive_nodeERNS0_INSt7__cxx114listEEE in ccEkVJyo.o
"__ZN5GiNaC6symbolC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
_main in ccEkVJyo.o
"__ZN5GiNaC9containerISt6vectorE8reg_infoE", referenced from:
__ZN5GiNaC9containerISt6vectorE21get_class_info_staticEv in ccEkVJyo.o
"__ZN5GiNaClsERSoRKNS_2exE", referenced from:
_main in ccEkVJyo.o
"__ZNK5GiNaC12archive_node14find_ex_by_locEN9__gnu_cxx17__normal_iteratorIPKNS0_8propertyESt6vectorIS3_SaIS3_EEEERNS_2exERNS_9containerINSt7__cxx114listEEE", referenced from:
__ZN5GiNaC9containerISt6vectorE12read_archiveERKNS_12archive_nodeERNS0_INSt7__cxx114listEEE in ccEkVJyo.o
"__ZNK5GiNaC12archive_node19find_property_rangeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_", referenced from:
__ZN5GiNaC9containerISt6vectorE12read_archiveERKNS_12archive_nodeERNS0_INSt7__cxx114listEEE in ccEkVJyo.o
"__ZNK5GiNaC5basic10eval_ncmulERKSt6vectorINS_2exESaIS2_EE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic11to_rationalERSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic13contract_withEN9__gnu_cxx17__normal_iteratorIPNS_2exESt6vectorIS3_SaIS3_EEEES8_RS7_", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic13to_polynomialERSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic14subs_one_levelERKSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEEj", referenced from:
__ZNK5GiNaC9containerISt6vectorE4subsERKSt3mapINS_2exES4_NS_10ex_is_lessESaISt4pairIKS4_S4_EEEj in ccEkVJyo.o
"__ZNK5GiNaC5basic5matchERKNS_2exERSt3mapIS1_S1_NS_10ex_is_lessESaISt4pairIS2_S1_EEE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic6normalERSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEES9_RNS_9containerINSt7__cxx114listEEE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC9containerISt6vectorE4infoEj", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I know that Undefined Symbols error often comes up because of the link error, but the detailed error messages looks like that these errors are from ginac itself.
I have tried different orders of these -l parameters, but the errors always exist.
I tried clang instead of gcc to compile this file and it works. So I guess I do install the ginac library correctly. But I'm still confused about these errors when using gcc.
Has anyone met this problem?
I would be very appreciate if anyone could give me some advice!
I also had issues using:
gcc-11 -lstdc++ -lginac -lcln hello.cpp -o hello -I /usr/local/include -L /usr/local/lib
but it did work with:
g++ -o hello hello.cpp `pkg-config --cflags --libs ginac`

Undefined symbols for architecture x86_64: linking mistake?

I am new to cpp, want to have a implementation of particle filter, I try to run the code here https://github.com/NewProggie/Particle-Filter, which is a structured and easy understanding project. But when I try to compile and link:
g++ $(pkg-config --cflags --libs opencv) -I/usr/local/Cellar/opencv3/3.1.0_1/include -I /usr/local/Cellar/gsl/1.16/include -stdlib=libc++ main.cpp -o main
I have following linking problem:
Undefined symbols for architecture x86_64:
"colorFeatures::colorFeatures()", referenced from:
_main in main-2b4c23.o
"colorFeatures::~colorFeatures()", referenced from:
_main in main-2b4c23.o
"adaboostDetect::detectObject(_IplImage*, CvRect**)", referenced from:
_main in main-2b4c23.o
"adaboostDetect::adaboostDetect()", referenced from:
_main in main-2b4c23.o
"tracker::addObjects(_IplImage*, CvRect*, int)", referenced from:
_main in main-2b4c23.o
"tracker::initTracker(_IplImage*, CvRect*, int, int)", referenced from:
_main in main-2b4c23.o
"tracker::showResults(_IplImage*)", referenced from:
_main in main-2b4c23.o
"tracker::next(_IplImage*)", referenced from:
_main in main-2b4c23.o
"tracker::tracker()", referenced from:
_main in main-2b4c23.o
"tracker::~tracker()", referenced from:
_main in main-2b4c23.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any kind person has ideas about this problem? Thanks in advance
have gsl installed properly
B) pass to g++ a reference to the lib directory where the gsl libraries are located (probably something like /usr/lib or /usr/local/lib, these should both be default locations for the linker to search), and also to where the header files are, and also tell the linker to do the linking.
g++ -o <name of executable> -L/path/to/gsl/libs -I/path/to/headers -lgsl <name of source file>
the -L tells it where to find the libraries (.so files on linux, .dylib on OS X), -I tells it where to find the headers, -l (that's a lower case L) tells it to link to the library, which would be named libgsl.so or libgsl.dylib.
First just try adding the -lgsl flag, then if it can't find libgsl.so (or .dylib), add the -L flag. NOTE: /path/to/gsl/libs and /path/to/headers are not what you should literally put in there, but replace them with the actual paths on your system.

Undefined symbols for architecture x86_64:"cv::face::createFisherFaceRecognizer(int, double)", referenced from: _main in main.o

I am working on facial recognition in video using opencv. I followed the tutorial on opencv's website(http://docs.opencv.org/modules/contrib/doc/facerec/tutorial/facerec_video_recognition.html). I get following error when I try to build my code in xcode.
Undefined symbols for architecture x86_64:
"cv::face::createFisherFaceRecognizer(int, double)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation).
I made a few changes to the code as given in here fatal error: opencv2/contrib/contrib.hpp' file not found (open cv already built)
Please guide me through it as I am new to opencv.
Thanks in advance!

libcurl issue when using D's std.net.curl on Mac

I'm trying to get up to speed using libcurl with D on Mac.
I'm using the latest dmd2 compiler (DMD64 D Compiler v2.062).
The following example compiles and works fine on Windows:
import std.net.curl, std.conv, std.stdio;
void main() {
string content = to!string(get("dlang.org"));
writeln(content);
}
When compiling it on Mac I get the following output:
~/src $ dmd dcurl.d
Undefined symbols for architecture x86_64:
"_curl_easy_cleanup", referenced from:
_D3std3net4curl4Curl8shutdownMFZv in libphobos2.a(curl_1522_37c.o)
"_curl_easy_duphandle", referenced from:
_D3std3net4curl4Curl3dupMFZS3std3net4curl4Curl in libphobos2.a(curl_151e_149.o)
"_curl_easy_init", referenced from:
_D3std3net4curl4Curl10initializeMFZv in libphobos2.a(curl_151d_432.o)
"_curl_easy_perform", referenced from:
_D3std3net4curl4Curl7performMFbZi in libphobos2.a(curl_1528_2fb.o)
"_curl_easy_setopt", referenced from:
_D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionlZv in libphobos2.a(curl_1525_14c.o)
_D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionAxaZv in libphobos2.a(curl_1524_14c.o)
_D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionPvZv in libphobos2.a(curl_1526_14c.o)
_D3std3net4curl4Curl5clearMFE3etc1c4curl10CurlOptionZv in libphobos2.a(curl_1527_207.o)
"_curl_easy_strerror", referenced from:
_D3std3net4curl4Curl11errorStringMFiZAya in libphobos2.a(curl_1520_4a1.o)
"_curl_global_cleanup", referenced from:
_D3std3net4curl4Curl19_sharedStaticDtor29FZv in libphobos2.a(curl.o)
"_curl_global_init", referenced from:
_D3std3net4curl4Curl19_sharedStaticCtor28FZv in libphobos2.a(curl.o)
"_curl_slist_append", referenced from:
_D3std3net4curl3FTP3dupMFZS3std3net4curl3FTP in libphobos2.a(curl_1518_ea.o)
_D3std3net4curl3FTP10addCommandMFAxaZv in libphobos2.a(curl_1518_ea.o)
_D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP in libphobos2.a(curl_1517_140.o)
_D3std3net4curl4HTTP16addRequestHeaderMFAxaAxaZv in libphobos2.a(curl_1517_140.o)
"_curl_slist_free_all", referenced from:
_D3std3net4curl3FTP4Impl6__dtorMFZv in libphobos2.a(curl_1518_ea.o)
_D3std3net4curl3FTP13clearCommandsMFZv in libphobos2.a(curl_1518_ea.o)
_D3std3net4curl4HTTP4Impl6__dtorMFZv in libphobos2.a(curl_1517_140.o)
_D3std3net4curl4HTTP19clearRequestHeadersMFZv in libphobos2.a(curl_1517_140.o)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
--- errorlevel 1
Which seems to imply a linking issue.
I have libcurl at:
/usr/lib/libcurl.3.dylib /usr/lib/libcurl.4.dylib /usr/lib/libcurl.dylib
But I'm not even sure whether dmd is trying to use them.
I've done this before with C/C++ (on the same machine using these libraries) so I think it must be a versioning issue or else I need to pass some flags to the compiler.
Any help would be most appreciated.
You need to link against libcurl. It's not linked against by default. So, instead of
dmd dcurl.d
you need to do
dmd -L-lcurl dcurl.d

Lear Gist Descriptor C code used with C++

I am using Lear's implementation of the Gist descriptor for a project which can be found here: http://lear.inrialpes.fr/software.
I am writing an application in c++ and I want to use this library. I am having issues though with the makefile and linking in general.
These commands give me no errors:
g++ -c standalone_image.c -o standalone_image.o
g++ -c gist.c -o gist.o
However, this line
g++ compute_gist.c `pkg-config --cflags --libs opencv`
gives me the following error
Undefined symbols for architecture x86_64:
"color_gist_scaletab(color_image_t*, int, int, int const*)", referenced from:
_main in ccMFYbAU.o
"color_image_delete(color_image_t*)", referenced from:
_main in ccMFYbAU.o
"color_image_new(int, int)", referenced from:
load_ppm(char const*)in ccMFYbAU.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I have the Mosaic c++ code in a different directory. I also tried to compile gist and standalone_image seperately, copy into the mosaic directory, and compile the Mosaic code.
Which gives me the following error:
Undefined symbols for architecture x86_64:
"color_gist_scaletab(color_image_t*, int, int, int const*)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [mosaic] Error 1
I really want to use this library in my project, but I can't figure out a way to incorporate it in my c++.
Any help is GREATLY appreciated! Thanks!
Edit: I am using Mac Lion with:
gcc + g++ version: i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
The lear library also uses the FFTW3 library that should work with C and C++.
The problem was that I needed the extern around the gist include, but the linking that was done in the Makefile was also wrong. It works now. :)