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>
Related
I need to include GLFW header in my c++ program. The installation was fine and the GLFW folder exists in my usr\include folder and g++ does look for header files in that folder. Despite that it throws an error telling me that the GLFW directory doesnt exist.
I am using sublime text as my editor and IDE and my system is Ubuntu-20.04
FOllowing is the code, the terminal command i used to compile and the error message i encountered:
#include <GLFW\glfw3native.h>
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout << "All DOne!!" << std::endl;
return 0;
}
g++ -g -o bin/debug/main src/*.cpp -x64 -std=c++17 -Wall -I -include -lglfw3 -lGL -lm -lXrandr -lXi -lX11 -lXxf86vm -lpthread && ./bin/debug.main
src/main.cpp:1:10: fatal error: GLFW\glfw3.h: No such file or directory
1 | #include <GLFW\glfw3.h>
| ^~~~~~~~~~~~~~
compilation terminated.
I cannot tell where the problem lies. please help.
and my system is Ubuntu-20.04
On Linux, the path delimiter is /, not \, so
#include <GLFW/glfw3native.h>
Note that while windows primarily uses backslash, it also accepts the forwad slash, so always using / is also the best option for cross-platform programming.
I am trying to compile my Qt app from console. Here is specs:
MCBC 3.0 (has preinstalled Qt and gcc), Qt 4.6.4, gcc v4.1.2, qmake v2.01a.
My test qt app contains only 2 files: basket.pro and main.cpp.
basket.pro:
TARGET = basket
TEMPLATE = app
SOURCES += main.cpp
HEADERS +=
main.cpp:
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
return a.exec();
}
I need to compile that app from console.
I got 2 virtual machines. VM #1 has installed QtCreator, and QtCreator compile this app without troubles, also app can be compiled from the console by "qmake" and "make". But there is VM#2, which does not have installed QtCreator, and it is where problems come from.
I tried compile app by commands "qmake make", trying to use Makefile, created by QtCreator, trying to use only gcc comands with explicitly defined paths to Qt libs, include-files and linked libs, but always I got the same errors. For example, I use 2 gcc comands, which used by QtCreator for successful compile this app:
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/lib/qt46/mkspecs/linux-g++ -I. -I/usr/lib/qt46/include/QtCore -I/usr/lib/qt46/include/QtGui -I/usr/lib/qt46/include -I. -o main.o main.cpp
g++ -o basket main.o -L/usr/lib/qt46/lib -lQtGui -L/usr/lib/qt46/lib -lQtCore -lpthread
And so after executing second command I got same error as always:
[root#ARM basket]# ./build_g++_step2
main.o: In function `basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::data(void) const':
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::QApplication(int &, char **, int)'
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::exec(void)'
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::~QApplication(void)'
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::~QApplication(void)'
collect2: ld returned 1 exit status
Please tell what I missed.
Your GCC compiler is version 2.95 (a much too old version from the previous century, which is not conforming to C++11 standard, and which was known to be quite buggy), since /usr/lib/gcc-lib/i586-linux/2.95.4/is used. Run simply g++ -v or g++ --version to check the version of your compiler. (You could have some PATH issue, or you could have misinstalled your build tools).
You should upgrade your compiler to some much newer version (at least GCC 6 and preferably GCC 7 in july 2017)
BTW, I recommend upgrading also your Qt library. Current version (in july 2017) is Qt5.9 and you should use some Qt5.
Then you need to regenerate your makefile, adding more in QT, as answered by ilbeldus
Notice that you could use pkg-config to help compiling Qt applications (perhaps in your Makefile). You still need to explicitly use moc. For example you might compile some Qt C++ GUI file foo.cc (after having used moc) with e.g.
g++ -Wall -g -c $(pkg-config --cflags Qt5Gui) foo.c
and you would use $(pkg-config --libs Qt5Gui) for link options.
I don't recommend using your old MCBC thing (even with googling I didn't find what that is), notably if it is so obsolete.
You are missing the setup of the QT variable in your .pro file.
Add QT += core gui to it, re-run qmake and try to recompile
See the docs here: http://doc.qt.io/qt-4.8/qmake-variable-reference.html#qt
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
So, I was following a simple C++ with SDL tutorial for linux but i encounter some errors on my way.
First of all I'm using Geany and i downloaded the corresponding SDL2 libs, here is the thing:
in my project folder there is a main.cxx file, which i open with geany as i mentioned before:
I included this libraries:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
First i encountered a pelculiar error, compilation performs sucessfully but when it comes to build i got this error:
main.cxx: undefined reference to `SDL_Init'
After searching a bit i found out that i had to add the tag -lSDL to my geany build options so they would end up being somethinf like this:
Compile:
g++ -Wall -c -lSDL "%f"
Build:
g++ -Wall -o -lSDL "%e" "%f"
But there is a problem, now when I execute the build command i get a:
G ++: error: main: There is no such file or directory
Why am i getting this error, am I including a wrong library or g++ has problems with .cxx files?
I already tried converting between .cxx and .cpp.
Thanks in advance.
g++ -Wall -c -lSDL2 "%f"
There is absolutely no need to specify libraries during compilation phase. Remove -lSDL.
g++ -Wall -o -lSDL2 "%e" "%f"
It invokes compiler, implies linking (no -c or other operation-specific flags), and sets output file name to -lSDL2. That is, linker will output resulting binary in a file named -lSDL2 in current working directory. Then, when it comes what files to link, it goes main, which supposed to be -o main, but since you've broken flags order it is now just ordinary file name that linker will try to link into resulting binary. It so happens that this file doesn't exist.
Long story short, make correct linking line - g++ -o "%e" %f -lSDL2 (libraries comes last, library order is also important).
For a C++ project I'd like to use ImageMagick to manipulate images. To test is out, my test1.cpp is just bare-bones :
#include <iostream>
#include <Magick++.h>
using namespace Magick;
int main(int argc, char * argv[]){
return 0;}
And I've been trying to compile it in the terminal by using :
g++ `Magick++-config --cxxflags --cppflags` -O2 -Wall -o test1 test1.cpp `Magick++-config --ldflags --libs`
and I get :
fatal error: 'Magick++.h' file not found
I've installed the latest version of ImageMagick (convert and identify work when called from the terminal) and the first lines of my .bash_profile look like this :
export MAGICK_HOME="/opt/ImageMagick-6.8.9"
export PATH="/System/Library/Frameworks:$MAGICK_HOME/bin:$PATH"
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib/"
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/opt/X12/lib/pkgconfig/:$MAGICK_HOME/lib/pkgconfig"
All path are correct and I have already used pkg-config to successfully link a c++ program to cairo.
What am I doing wrong ? What's the simpler way to use ImageMagick in a c++ program ?
(I use vim and the terminal and do not wish to use an IDE at this point).
Thanks in advance for any help !
-- EDIT --
Here's what's returned by Magick+-config (--cxxflags and --cppflags) :
-DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 -I$MAGICK_HOME/include/ImageMagick-6
And by --ldflags and --libs :
-L$MAGICK_HOME/lib -lMagick++-6.Q16 -lMagickWand-6.Q16 -lMagickCore-6.Q16