I'm trying to make a C++ script that will run some simple Python code:
// t.cpp
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
Upon running g++ t.cpp, I get the error:
t.cpp:1:20: fatal error: Python.h: No such file or directory
compilation terminated
I've found many similar questions, all specific to an IDE or other development software, or were solved by installing python3-dev. The python3-dev package is already installed, and I even tried manually including the header when attempting to compile:
g++ t.cpp -I ~/.virtualenvs/MainEnv/include/python3.5m/Python.h
g++ t.cpp -I /usr/include/python3.5m/Python.h
Neither changes anything.
How can I fix this error?
UPDATE: I found that using g++ t.cpp -I /usr/include/python3.5/ seems to include the header, but then it runs into more errors:
t.cpp:(.text+0x10): undefined reference to `Py_Initialize'
t.cpp:(.text+0x1f): undefined reference to `PyRun_SimpleStringFlags'
t.cpp:(.text+0x24): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
I've set up a similar example on my github
g++ t.cpp is missing a few things:
Tell g++ where the headers are for cpython (by -I/path/to/headers/)
Tell g++ to link against libpython (by -lpython3.5m)
You can also retrieve these flags with pkg-config
$ pkg-config python-3.5 --libs --cflags
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m -lpython3.5m
Your commandline should look something like g++ -I/usr/include/python3.5m t.cpp -lpython3.5m
#include <...> is for includes that come with the compiler.
Use #include "Python.h" for any other includes.
Run the following commands to compile your code:
mytest.cpp:
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
Compile:
$ g++ mytest.cpp `pkg-config python3-embed --libs --cflags` -o mytest
$ ./mytest
Related
I have downloaded the google test with below command.
wget https://github.com/google/googletest/archive/release-1.8.0.zip
and I run the following command to install the libraries to my macOS 10.13.5
unzip release-1.8.0.zip
cd googletest-release-1.8.0
mkdir build
cd build
cmake ..
make
sudo make install
and i try to compile the test as below code with command g++ -c -std=c++11 -stdlib=libc++ -lgtest -lgtest_main -pthread -o cpptest test.cpp.
#include <iostream>
#include <gtest/gtest.h>
TEST(firstTest, abs)
{
EXPECT_EQ(1, abs( -1 ));
EXPECT_EQ(1, abs( 1 ));
}
int main(int argc, char **argv) {
std::cout << "Running main() from testmain.cc\n";
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
but i get below warnings
clang: warning: -lgtest: 'linker' input unused [-Wunused-command-lin-argument]
clang: warning: -lgtest_main: 'linker' input unused [-Wunused-command-line-argument]
Does anyone can fix this problems?
g++ -c is used to compile a source file to an object file. This stage does not link an executable, i.e. it does not use linker, thus linker flags -lgtest -lgtest_main are not used. If you want to compile an executable the proper compand will be without -c:
g++ -std=c++11 -lgtest -lgtest_main -pthread -o cpptest test.cpp
Note, I have removed not required use of -stdlib.
I tried to build this, but always got link-time error.
#include <libavutil/log.h>
int main(int argc, char *argv[])
{
::av_log_set_flags(AV_LOG_SKIP_REPEATED);
return 0;
}
My distro is Debian GNU/Linux 8 (jessie). The FFmpeg was built by myself, and the configure command was...
$ ./configure --prefix=/usr/local --disable-static --enable-shared \
> --extra-ldflags='-Wl,-rpath=/usr/local/lib'
The link-error is as follows.
$ g++ foo.cpp -D__STDC_CONSTANT_MACROS -Wall \
> -Wl,-rpath=/usr/local/lib \
> $(pkg-config --cflags --libs libavutil)
/tmp/ccKzgEFb.o: In function `main':
foo.cpp:(.text+0x17): undefined reference to `av_log_set_flags(int)'
collect2: error: ld returned 1 exit status
where the output of pkg-config is...
$ pkg-config --cflags --libs libavutil
-I/usr/local/include -L/usr/local/lib -lavutil
The objdump shows that the shared object libavutil.so does have av_log_set_flogs inside.
$ objdump --dynamic-syms /usr/local/lib/libavutil.so | grep 'av_log_set_flags'
000260f0 g DF .text 0000000a LIBAVUTIL_54 av_log_set_flags
Please note that the g++ command used to build the above application had a linker option -Wl,-rpath=/usr/local/lib, though it still doesn't work. Also, I've tried to monitor with inotifywait if the other version provided by the distro were called. They were not, and the one being opened during execution of g++ was /usr/local/lib/libavutil.so.
Summary:
/usr/local/lib/libavutil.so does have the symbol.
-rpath was used to force to link against the shared library.
Why link-time error? T_T
Any suggestion or information would be highly appreciated! Thanks!
REEDIT: ffplay works fine and ldd shows it use /usr/local/lib/libavutil.so. So, the libraries seems not broken, and the problem becomes how to build my own codes to use the libraries.
This had me baffled too for a while. I managed to google this: http://soledadpenades.com/2009/11/24/linking-with-ffmpegs-libav/
It turns out FFMPEG don't make their header files C++ aware.
Here is the fix:
extern "C"
{
#include <libavutil/log.h>
}
int main(int argc, char *argv[])
{
::av_log_set_flags(AV_LOG_SKIP_REPEATED);
return 0;
}
You need to wrap all ffmpeg header includes with extern "C" linkage.
I've written a simple code which create folder. The problem is that I can't compile it. The code is below:
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::create_directories("/tmp");
return 0;
}
Compilation:
g++ createFolder.cpp -std=c++0x -lboost_system -o createFolder
I have got errors:
collect2: ld returned 1 exit status
How to correct the compilation process to run this program.
Try adding boost-filesystem to you linker:
g++ createFolder.cpp -std=c++0x -lboost_system -lboost_filesystem -o createFolder
I've compiled Qt on OSX Lion using the instructions provided at this official guide. I've then tried compiling the following Hello World with gcc hello_world.cpp -o hello_world
#include <QApplication>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
return app.exec();
}
I've got the following error:
hello_world.cpp:1:10: fatal error: 'QApplication' file not found
#include <QApplication>
^
1 error generated.
try instead #include <QtGui/QApplication>
Use -I option of gcc to give additional include locations.
gcc hello_world.cpp -I/path-to-qt/include -o hello_world
If you use it like that, you have to use your includes like this:
#include <QtGui/QApplication>
if you want your includes to be shorter like #include <QApplication>, you can give multiple include folders like this:
gcc hello_world.cpp -I/path-to-qt/include/QtCore -I/path-to-qt/include/QtGui -o hello_world
But that is not all. You also have to give library directories and which libraries to link to, which is done like this:
gcc hello_world.cpp -I/path-to-qt/include/QtCore -I/path-to-qt/include/QtGui -o hello_world -L/path-to-qt/lib -lQtCore -lQtGui
It is also better to use g++, since you are using C++.
g++ hello_world.cpp -I/path-to-qt/include/QtCore -I/path-to-qt/include/QtGui -o hello_world -L/path-to-qt/lib -lQtCore -lQtGui
Try with g++ -I<path_to_include_directory> -L<path_to_library_dir> -lQtCore.
For example, in my Debian I would do: g++ -I/usr/local/include/Qt4 -L/usr/local/lib -lQtCore -lQtGui whatever.cpp
EDIT: Thanks to #erelender to point out that QApplication is in the QtGui library and that it depends on QtCore.
What if you try to add additional include path for gcc with using -I flags? Something like:
gcc -I/usr/local/Qt-5.1.1/include hello_world.cpp -o hello_world
Not sure about the path in mac, but on Linux the class QApplication
is defined at following location (qt4)
/usr/include/qt4/QtGui/qwindowdefs.h
Do you have something similar on Mac?
If you are building from command line, including a header file with gcc can be done with following switch
-I<path to .h file>
For one reason or another, I am messing around with the exit() function in c++. I am getting all kinds of strange errors from my mac running lion (64 bit). I am compiling using g++ -o -g -Wall.
Exhibit A:
#include <iostream>
int main(int arc, char *argv[]){
exit(1);
}
The Terminal output looks like this
$ g++ -o -g -Wall test main.cpp
ld: in test, can't link with a main executable for architecture x86_64
collect2: ld returned 1 exit status
but $ g++ -o test main.cpp compiles fine.
using #include<stdio.h> or #include<stdlib.h> result in the same compilation error.
I am just wondering if anyone might be able to see immediately what is going on here?
test is the name of the binary to produce, your first argument list should be:
> g++ -g -Wall -o test main.cpp
^^^^^^^ -o has test for an argument
-o is meant to be followed immediately by the name of the output file. It is probably trying to use your old binary 'test' as a source file, incorrectly.
Try this:
g++ -o test -g -Wall main.cpp