Skip to content
Advertisement

Undefined reference to `main` error when embedding Python in C++

I’m trying to embed Python in C++. This is my Python file (with the name EmbedTest.py):

JavaScript

This is my C++ file (with the name EmbedTest.cpp and located in the same folder as EmbedTest.py)

JavaScript

Compiling is fine. I use the flags suggested by python3.6-config --cflags. Hence

gcc -c -I/home/MyFolder/anaconda3/include/python3.6m -Wno-unused-result -Wsign-compare -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -O3 -pipe -fdebug-prefix-map==/usr/local/src/conda/- -fdebug-prefix-map==/usr/local/src/conda-prefix -fuse-linker-plugin -ffat-lto-objects -flto-partition=none -flto -DNDEBUG -fwrapv -O3 -Wall -Wstrict-prototypes EmbedTest.cpp

works fine.

However, when I try to link, I get a problem. I use the flags suggested by python3.6-config --ldflags. Hence I try

gcc -o EmbedTest.o -L/home/MyFolder/anaconda3/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/home/MyFolder/anaconda3/lib -lpython3.6m -lpthread -ldl -lutil -lrt -lm -Xlinker -export-dynamic.

However, I get the following error message:

JavaScript

Any idea what goes wrong here?

Advertisement

Answer

You are not linking the .o file, you are outputing to it.

Change

JavaScript

to

JavaScript
Advertisement