undefined reference to `Py_Initialize'

2,608 views
Skip to first unread message

Munde Pravin

unread,
Nov 8, 2017, 9:16:20 AM11/8/17
to ns-3-users
i run the following program with ./waf --run scratch/sample

#include "python2.7/Python.h"

int main(int argc, char *argv[]) {

    Py_Initialize();
    PyRun_SimpleString("print \"Hello, world!\"");

    Py_Finalize();
    return 0;
}

and got the following error:

sratch/sample.cc:5: undefined reference to `Py_Initialize'
scratch/sample.cc:6: undefined reference to `PyRun_SimpleStringFlags'
scratch/sample.cc:8: undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status

it would be grateful to have suggestion from anyone


Tommaso Pecorella

unread,
Nov 10, 2017, 11:08:03 AM11/10/17
to ns-3-users
These don't seems to be ns-3 functions...

T.

Jenny Lee

unread,
Nov 29, 2017, 4:15:08 AM11/29/17
to ns-3-users
Hey, I got the same error, too. And here is my solution.
Since you are trying to use python methods in C++, proper libraries are needed. 
To find the missing libraries, use this:
python2.7-config --ldflags
Then you got library paths and library lists like this:
-L/usr/lib/python2.7/config-x86_64-linux-gnu -L/usr/lib -lpthread -ldl  -lutil -lm  -lpython2.7 -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
Add these libraries and paths into the wscript file and try again.

J

在 2017年11月8日星期三 UTC+8下午10:16:20,Munde Pravin写道:

Aftab hussain

unread,
Jul 2, 2022, 9:14:54 AM7/2/22
to ns-3-users
Hey
I have the same issue. could you please elaborate on where to add these paths?

I have these in the ns3 dir

ns3@ubuntu:~/workspace/ns-allinone-3.36.1/ns-3.36.1$ ls -l
total 996
-rw-rw-r--  1 ns3 ns3  12856 May 23 11:44 AUTHORS
drwxrwxr-x  3 ns3 ns3   4096 Jun 21 20:01 bindings
drwxrwxr-x 10 ns3 ns3   4096 Jul  2 16:49 build
drwxrwxr-x  4 ns3 ns3   4096 Jun 21 20:01 build-support
-rw-rw-r--  1 ns3 ns3 182274 May 23 11:48 CHANGES.md
drwxrwxr-x  9 ns3 ns3   4096 Jul  2 15:11 cmake-cache
-rw-rw-r--  1 ns3 ns3   5511 Jul  2 16:49 CMakeLists.txt
drwxrwxr-x  2 ns3 ns3   4096 Jun 21 20:01 contrib
-rw-rw-r--  1 ns3 ns3  18336 May 23 11:44 CONTRIBUTING.md
drwxrwxr-x  7 ns3 ns3   4096 Jun 21 20:05 doc
drwxrwxr-x 20 ns3 ns3   4096 Jun 29 12:41 examples
-rw-rw-r--  1 ns3 ns3  17987 May 23 11:44 LICENSE
drwxrwxr-x  2 ns3 ns3   4096 Jun 26 16:44 logs
-rw-rw-r--  1 ns3 ns3   1954 Jun 26 17:56 myfirst.tr
-rwxrwxr-x  1 ns3 ns3  57733 May 23 11:44 ns3
drwxrwxr-x  2 ns3 ns3   4096 Jun 21 21:48 __pycache__
-rw-rw-r--  1 ns3 ns3   4208 May 23 11:44 README.md
-rw-rw-r--  1 ns3 ns3 188478 May 23 11:48 RELEASE_NOTES.md
drwxrwxr-x  4 ns3 ns3   4096 Jun 30 10:27 scratch
-rw-rw-r--  1 ns3 ns3    506 Jul  2 17:02 simple-udp-0-0.tr
-rw-rw-r--  1 ns3 ns3    262 Jul  2 17:02 simple-udp-0-1.tr
-rw-rw-r--  1 ns3 ns3    256 Jul  2 17:02 simple-udp-1-0.tr
-rw-rw-r--  1 ns3 ns3    524 Jul  2 17:02 simple-udp-1-1.tr
-rw-rw-r--  1 ns3 ns3    258 Jun 29 12:46 simple-udp-2-0.tr
-rw-rw-r--  1 ns3 ns3    258 Jun 29 12:46 simple-udp-3-0.tr
drwxrwxr-x 46 ns3 ns3   4096 Jun 21 20:01 src
-rwxrwxr-x  1 ns3 ns3  82218 May 23 11:44 test.py
drwxrwxr-x  2 ns3 ns3   4096 Jun 21 22:08 testpy-output
-rw-rw-r--  1 ns3 ns3    131 May 23 11:44 testpy.supp
-rw-rw-r--  1 ns3 ns3 322880 Jun 26 19:45 udpclientserver.tr
drwxrwxr-x  4 ns3 ns3   4096 Jun 21 20:01 utils
-rw-rw-r--  1 ns3 ns3   4219 May 23 11:44 utils.py
-rw-rw-r--  1 ns3 ns3      7 May 23 11:48 VERSION
ns3@ubuntu:~/workspace/ns-allinone-3.36.1/ns-3.36.1$


I don't have ./waf, instead I run as ./ns3 run scratch/sample.cc


I'm pretty new to ns3. In my university project, I have a use case to run python code from the inside od ns3 application which is written in Cpp

Thanks in advance
Reply all
Reply to author
Forward
0 new messages