I'm trying to embed python code in C++ (Windows 7 + minGW + Python 2.7.2 + Eclipse Indigo with CDT and PyDev).
So, this is the simple code:
#include <Python.h> //Python.h
#include <iostream> //iostream
using namespace std;
int main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
Py_Finalize();
return 0;
}
And I couldn't understant what am I doing wrong.
I include dirrctories C:\Python27\include and C:\Python27\libs but I can't build my project.
1) When I trying to build my project I got this error:
**** Internal Builder is used for build **** g++
-IC:\Python27\include -IC:\Python27\libs -O0 -g3 -Wall -c
-fmessage-length=0 -o main.o ..\main.cpp g++ -o testpy2.exe main.o
main.o: In function `main':
C:\Users\const\workspace\testpy2\Debug/../main.cpp:7: undefined
reference to `_imp__Py_Initialize'
C:\Users\const\workspace\testpy2\Debug/../main.cpp:9: undefined
reference to `_imp__PyRun_SimpleStringFlags'
C:\Users\const\workspace\testpy2\Debug/../main.cpp:10: undefined
reference to `_imp__Py_Finalize'
collect2: ld returned 1 exit status
Build error occurred, build is stopped Time consumed: 1507 ms.
2) And if I change current toolchain in Eclipse from "minGW" to "CrossGCC" .. I got this error:
**** Build of configuration Release for project testpy ****
make all Building file: ../main.cpp Invoking: Cross G++ Compiler g++
-I"C:\Python27\include" -I"C:\Python27\libs" -O3 -Wall -c
-fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o"
"../main.cpp" Finished building: ../main.cpp Building target:
testpy.exe Invoking: Cross G++ Linker g++ -o "testpy.exe" ./main.o
-l"C:/Python27/libs/libpython27.a" -l"C:/Python27/libs/python27.lib"
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:
cannot find -lC:/Python27/libs/libpython27.a
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:
cannot find -lC:/Python27/libs/python27.lib collect2: ld returned 1
exit status make: *** [testpy.exe] Error 1
**** Build Finished ****
Could anybody tell me what's wrong with my code or settings or something else?
Thank you
That is a linker error, not a compiler error. You need to link to the python. As you can see, with the "CrossGCC" toolchain you are almost there:
-lC:/Python27/libs/libpython27.a
You need to change this to
-LC:/Python27/libs -lpython
Related
I am using a new install of cygwin with glut32 libraries on a Windows 7 64-bit machine. I am trying to put together a test program in C++ using the Eclipse IDE. Standard terminal programs and a test GLUT32 program build and run as expected. I am trying to hide the console window on a release build of my test program.
I attempted to use the -mwindows flag in the linker step to suppress the console window, but I get the following:
11:15:04 **** Incremental Build of configuration Release for project Test ****
make all
Building file: ../src/Test.cpp
Invoking: Cygwin C++ Compiler
g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Test.d" -MT"src/Test.o" -o "src/Test.o" "../src/Test.cpp"
Finished building: ../src/Test.cpp
Building target: Test.exe
Invoking: Cygwin C++ Linker
g++ -L"C:\cygwin\lib" -Xlinker -mwindows -shared -o "Test.exe" ./src/Test.o -lglut32 -lglu32 -lopengl32
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../x86_64-pc-cygwin/bin/ld: unrecognised emulation mode: windows
Supported emulations: i386pep i386pe
collect2: error: ld returned 1 exit status
make: *** [makefile:47: Test.exe] Error 1
11:15:05 Build Finished (took 376ms)
I think the supported emulation statement might be a hint. Am I somehow using the wrong compiler? How do a build a finished program without having a console windows pop up in the background?
I can't explain why, but the problem is solved when I use the -mwindows flag in the compilation step as opposed to the linking step. Program builds with no problems and runs without the console.
I am trying to run a really simple GUI application in C++ with Eclipse (Neon): the program starts, shows a red display and closes itself after 10 seconds.
To achieve this I am running the Allegro 5.0.10 game engine, its source code installs some libs inside /usr/local/include/allegro5. My program looks like this:
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro5.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(255,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
Creating a new project from scratch with the following options...
...and building it with these ones...
...when selecting 'Build All', an error message appears in the console:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -fPIC -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: pang
Invoking: GCC C++ Linker
g++ `pkg-config --libs allegro-5 allegro_image-5` -o "pang" ./main.o
./main.o: In function `main':
/home/xvlaze/workspace/pang/Debug/../main.cpp:14: undefined reference to `al_install_system'
/home/xvlaze/workspace/pang/Debug/../main.cpp:19: undefined reference to `al_create_display'
/home/xvlaze/workspace/pang/Debug/../main.cpp:25: undefined reference to `al_map_rgb'
/home/xvlaze/workspace/pang/Debug/../main.cpp:25: undefined reference to `al_clear_to_color'
/home/xvlaze/workspace/pang/Debug/../main.cpp:27: undefined reference to `al_flip_display'
/home/xvlaze/workspace/pang/Debug/../main.cpp:29: undefined reference to `al_rest'
/home/xvlaze/workspace/pang/Debug/../main.cpp:31: undefined reference to `al_destroy_display'
collect2: error: ld returned 1 exit status
make: *** [pang] Error 1
EXTRA: I have already reproduced this answer, but it's still not working.
The problem you are now having is that the special flags you have added are coming before the object that depends on them.
What you should do is change the GCC C Linker -> Command line pattern to have ${FLAGS} after the ${INPUTS}.
Doing that will change the compile line from:
g++ `pkg-config --libs allegro-5 allegro_image-5` -o "pang" ./main.o
to:
g++ -o "pang" ./main.o `pkg-config --libs allegro-5 allegro_image-5`
See https://stackoverflow.com/a/409470/2796832 for some more info on link order and why it matters.
#include <iostream>
#include <Magick++.h>
int main()
{
Magick::InitializeMagick(NULL);
Magick::Image im;
im.read("/home/chase/Desktop/m42.jpg");
im.display();
return 0;
}
I get the following error when I try to compile in eclipse...
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/include/x86_64-linux-gnu/ImageMagick-6 -I/usr/include/ImageMagick-6 -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: Stacking
Invoking: GCC C++ Linker
g++ -L../ -o "Stacking" ./main.o -lMagick++-6.Q16 -lMagickWand-6.Q16 -lMagickCore-6.Q16 -lMagick++-6.Q16 -lMagickWand-6.Q16 -lMagickCore-6.Q16
./main.o: In function `main':
/home/chase/workspace/Stacking/Debug/../main.cpp:8: undefined reference to `Magick::Image::read(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
makefile:44: recipe for target 'Stacking' failed
make: *** [Stacking] Error 1
Why does the read function not compile. I used the package config tool in eclipse to set up Magick++. Also as far as I can tell all the other functions work fine. I am using Ubuntu. I installed Magic++ using sudo apt-get install libmagick++-dev.
Update:
I got it to work. I had upgraded to g++-5. When I compiled with g++-4.9 it worked. Wonder why it does not work with g++-5?
Why does the read function not compile
Your read function does compile. What you have is a link problem.
why it does not work with g++-5
Because g++-4.x and g++-5.x use different ABI, and are not link-compatible (and your libMagick* libraries were built with g++-4.x).
I am using Netbeans for my C++ project. I compiled my program using make and ran into this error:
collect2: error: ld terminated with signal 11 [Segmentation fault], core dumped
Makefile:4: recipe for target 'barn' failed
make: *** [barn] Error 1
Whereas, when I compiled it in a linux environment(Ubuntu to be precise), it compiled fine. What could have possibly gone wrong?
This is what I got when I typed make -n:
g++ -c main.cc
g++ -c Animal.cc
g++ -c Bird.cc
g++ -c Chicken.cc
g++ -c Cat.cc
g++ -c Pig.cc
g++ -o barn main.o Animal.o Bird.o Chicken.o Cat.o Pig.o Random.o
PS I prefer using Netbeans
A segment fault in the linker suggests a bug with that. This is what I would try if I were to run into this problem.
At the command line do
g++ -o barn main.cc Animal.cc Bird.cc Chicken.cc Cat.cc Pig.cc Random.cc
If that does not work, try variations like:
g++ -o barn main.cc Pig.cc Random.cc Animal.cc Bird.cc Chicken.cc Cat.cc
The order should not matter. This is just the kind of thing I would try with a mystery-meat problem like this.
Recently I need to use zlib in programming on Ubuntu and here I have a problem.
I wrote a small program to test zlib functions and I built it in Eclipse.
I found it can be compiled but there were some linking errors like this:
main.cpp:27: undefined reference to `compress'
main.cpp:38: undefined reference to `uncompress'
What should I do with this? If this is because there is no library file?
Can anyone give some help? Thanks a lot!
MOODY_Y
Plus, here are my building info:
11:59:08 **** Build of configuration Debug for project test_zlib ****
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: test_zlib
Invoking: GCC C++ Linker
g++ -o "test_zlib" ./main.o
./main.o: In function `main':
/home/hyq/workspace/test_zlib/Debug/../main.cpp:27: undefined reference to `compress'
/home/hyq/workspace/test_zlib/Debug/../main.cpp:38: undefined reference to `uncompress'
collect2: ld return 1
make: *** [test_zlib] error 1
11:59:10 Build Finished (took 1s.715ms)
Try:
sudo apt-get install zlib1g-dev
Also, use -lz on the compile/link command.
You need to link youe source to zlib.Build your code as follows
gcc main.cpp -lz