简体   繁体   中英

Python 3.3 undefined references with static linking

Operating System: Ubuntu 12.04 x86_64

I've compiled libpython3.3m.a from source, along with the related headers. I wrote the following Makefile (specifically, look at my CFLAGS variable, which includes -lpython3.3m)...

BINS=python python-d
SOURCES=python.cpp

LIBROOT=/home/uberblah/lib/cpp
LIBDIR=$(LIBROOT)/lib
BINDIR=$(LIBROOT)/bin
INCDIR=$(LIBROOT)/include

CFLAGS= -I$(INCDIR)/python3.3m -L$(LIBDIR) -lpython3.3m

all: $(BINS)

python-d: $(SOURCES)
    g++ $(CFLAGS) -g -o $@ $^

python: $(SOURCES)
    g++ $(CFLAGS) -o $@ $^

clean::
    rm -r -f *~ $(BINS)

edit::
    gedit $(SOURCES) Makefile &

And I wrote the following .cpp source file...

#include <Python.h>

int main(int argc, char** argv)
{
  Py_SetProgramName((wchar_t*)argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print 'Today is',ctime(time())\n");
  Py_Finalize();
  return 0;
}

Typing make in the Makefile's directory results in the following messages.

g++ -I/home/uberblah/lib/cpp/include/python3.3m -L/home/uberblah/lib/cpp/lib -lpython3.3m -o python python.cpp
/tmp/ccBD4sLY.o: In function `main':
python.cpp:(.text+0x1a): undefined reference to `Py_SetProgramName'
python.cpp:(.text+0x1f): undefined reference to `Py_Initialize'
python.cpp:(.text+0x2e): undefined reference to `PyRun_SimpleStringFlags'
python.cpp:(.text+0x33): undefined reference to `Py_Finalize'
collect2: ld returned 1 exit status
make: *** [python] Error 1

Based on my understanding of the '-l' argument for g++, it is finding libpython3.3m.a, because otherwise it would tell me it was unable to find the library and exit.

So, if it's finding the library, why aren't these functions defined?

ADDITIONAL NOTES...

Built Python using --prefix and altinstall to have it install to a new directory (the one I'm pointing LIBROOT to in my makefile).

I have confirmed that libpython3.3m.a is in $(LIBDIR)

using nm to check for Py_SetProgramName...

uberblah@uberblah-N80Vm:~/lib/cpp/lib$ nm libpython3.3m.a | grep Py_SetProgramName
                 U Py_SetProgramName
0000000000001c00 T Py_SetProgramName
                 U Py_SetProgramName

Running Make verbosely...

    ...
    Finished prerequisites of target file `python'.
  Must remake target `python'.
g++ -I/home/uberblah/lib/cpp/include/python3.3m -L/home/uberblah/lib/cpp/lib -lpython3.3m -o python python.cpp
Putting child 0x00a585b0 (python) PID 16811 on the chain.
Live child 0x00a585b0 (python) PID 16811 
/tmp/cc2dom1S.o: In function `main':
python.cpp:(.text+0x1a): undefined reference to `Py_SetProgramName'
python.cpp:(.text+0x1f): undefined reference to `Py_Initialize'
python.cpp:(.text+0x2e): undefined reference to `PyRun_SimpleStringFlags'
python.cpp:(.text+0x33): undefined reference to `Py_Finalize'
collect2: ld returned 1 exit status
Reaping losing child 0x00a585b0 PID 16811 
make: *** [python] Error 1
Removing child 0x00a585b0 PID 16811 from chain.

If I split the compilation into an object stage then a binary stage, the binary stage fails with the same error message as originally mentioned.

Found the solution: Python actually installs with its own solution to this. It's talked about in this post (I knew my header wasn't missing, but I was being dumb and trying to link it all up myself, thinking "Oh, this is just what anyone needs to do to get Python working on my platform")...

Python.h header missing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM