Setup Qt for cross compile for raspberry pi - c++

I want to cross compile a project from my linux machine to a raspberry pi using Qt creator. Now I have set up a kit with compilers (that I tested to work) and a sysroot. However when I try to compile a simple "Hello world" example in Qt creator I get an error:
#include <iostream>
using namespace std;
int main()
{
cout << "Hallo!!!" << endl;
return 0;
}
/usr/include/c++/5/iostream:38: from /usr/include/c++/5/iostream:38,
~/sysroot/usr/include/features.h:364: error: sys/cdefs.h: No such file or directory
I see that Qt tries to use uses the following file for compilation:
/usr/include/c++/5/iostream
However it should use ~/sysroot/usr/include/c++/6/iostream
In the kit I use I have specified ~/sysroot as the sysroot, but it seems to use the other c++ includes. How can I change this?
EDIT: I have found a solution for the problem. See answers below. However I'm not sure if this is the best solution. I there are better ways, please let me know.

I found the problem. The default qmake build step adds the argument " -spec linux-g++" to the call to qmake. However this causes the wrong compiler to be chosen. Since I cannot remove the argument from the qmake build step, I removed the qmake step and added a custom build step which calls qmake and omits the -spec argument. This seems to work, but it feels a little dirty. Maybe there are better/cleaner solutions for this?

Related

Why does string type cause c++ program to not run?

I have a simple program, written in C++, on a Windows 10 machine, compiled with the MinGW g++ compiler.
I am including the <string> header, and it runs fine. When I include the string data type, it will compile, but it will not run.
The minimum amount of code to reproduce this is:
#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting;
cout << "hi" << endl;
return 1;
}
Please note, this works fine:
#include <iostream>
#include <string>
using namespace std;
int main() {
//string greeting;
cout << "hi" << endl;
return 1;
}
The version of the compiler is 6.3.0
g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
I downloaded it 3 days ago, so I assume it is the most recent version. (Maybe not?)
I have googled and browsed stackoverflow for answers.
The closest question I could find, the person gave up and changed operating systems.
One other solution I found was to use Cygwin's compiler. I would rather not, as I already have a seemingly otherwise fine compiler. It seems that MinGW tools are widely used enough that I should be able to use the compiler.
Any other forum/blog/etc resources have problems about converting strings or calling string methods.
NOTE: The same exact code runs fine on Ubuntu 16.04, with the included compiler.
EDITS:
While I realize that "It does not run" is not helpful, I don't know how else to describe it. I run the compiled program, and it behaves the same as if I entered echo ''. There is no output, no indication that anything has happened. What is the most helpful way to phrase that behavior?
If I use a debugger, I get program exited with code 0xc0000139
A quick google search returns results indication that it is a problem with the compiler. Same as the comments below about my compiler version...
To compile the program, I run g++ main.cpp -o b.exe
To execute it, I run ./b.exe
Mingw has a long standing issue with certain consoles (see their faq). It might be worth checking whether it's failing to output rather than execute by e.g. redirecting to a file ./b.exe >out.txt.
After some useful comments, and much frustration, I decided to try to build the most current compiler. I caved and used Cygwin. (I really have no problem with Cygwin, I just wanted to get MinGW to work.)
I followed this site's instructions (after downloading the current source from https://gcc.gnu.org).
http://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/
Notes:
This is for an older version of gcc, but I replaced the appropriate version numbers with 8.2.0 (current at this time).
I was missing some prerequisites, and had to cd into the source directory and run ./contrib/download_prerequisites from the Cygwin terminal. It handled everything seamlessly, and while it took a little bit, I am now able to use string data types and run the program (successfully).
Don't forget the final make install command. I did, and it was a headache.
Lastly, thanks to all the helpful comments, and those that asked for clarification. When I call the mechanic, and say, "my car won't run", they ask helpful questions, and we work together to get them the relevant information they need. That's what happened above, and I learned some things (and solved my issue).
I had the same issue. I downloaded mingw a few days from sourceforge and everything complied fine except when I declared a string. Then it would show no output. Maybe it was some problem with the old version (6.3.0). I deleted all the files and re-installed using the given tutorial : https://code.visualstudio.com/docs/cpp/config-mingw . This is a newer version (8.1.0). Now it works just fine!

gcc and clang under msys2 cannot resolve includes with absolute paths

I try to get tests generated by the cxxtest framework working under a MinGW environment managed by mysys2. The tool generates C++ files with absolute paths. However, gcc seems to be unable to resolve this absolute paths.
Here is a minimal example to demonstrate the problem:
// file1.h
#include <iostream>
inline void hallo() { std::cout << "Hallo\n"; }
// main.cpp
#include "/home/phil/example/file1.h"
int main()
{
hallo();
return 0;
}
The file exists (at least the msys2 shell resolves the path):
$ ls /home/phil/example/file1.h
/home/phil/example/file1.h
... but calling g++ results in this error:
$ g++ main.cpp
main.cpp:1:38: fatal error: /home/phil/example/file1.h: No such file or directory
#include "/home/phil/example/file1.h"
^
compilation terminated.
Same error with clang.
Under a full Linux environment, the example works. It also works if I replace the absolute path by a relative one (#include "file1.h").
So, I assume the problem lies in the layer over Windows that is responsible to resolve paths. Not sure whether I should report it as a bug to the msys2 project, or whether it is a known problem. If it is a known problem, are there any workarounds (like setting -I options)?
(If possible, I would like to avoid replace the absolute paths, as they are in generated code by the cxxtest framework. Technically, running a postprocessing step on the generated files would be possible but seems like a hack in the long run.)
Since you are running compilers that use MinGW-w64 as their runtime environment, they don't recognize POSIX-style paths like that. I think they actually interpret the root directory "/" to be "C:\". Other than that, they would only recognize native Windows-style paths.
I recommend that you pass the argument -I/home/phil/example to your compiler from some program running in the msys-2.0.dll POSIX emulation runtime environment (e.g. /usr/bin/bash or /usr/bin/make). The msys-2.0.dll runtime will then convert that argument to use a native Windows path so the compiler can understand it, and statements like #include <file1.h> will work. Alternatively, you might try putting a Windows-style path in your source code, e.g. the path should start with C:\.
Note however that having absolute paths in source code or build scripts is a bad idea since it makes it harder to build the code on a different computer. You could consider using environment variables or relative paths.
Try using the MinGW compiler that Cygwin provides as a package. (In other words, forget the MSYS environment; work under Cygwin, but build the code as before, in the MinGW style.)
Then you should be able to have include references /home/phil; it will just resolve to C:\Cygwin\home\phil or wherever your Cygwin root is.
Actually, it might be possible under MSYS also (which, after all, is just the descendant of an old for of Cygwin). You just have to figure out what /home/phil is referring to, create that tree and work under there.

fatal error: iostream: No such file or directory 3

A number of answers to this exact error have been put upon this website but I am quite the beginner to C++ and Code::Block so i'm afraid I do not understand them.
I have been following a very simple C++ tutorial that started me out with one simple program that I was told to copy and paste into the compiler.
#include <iostream>
using namespace std;
int main()
{
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
cin.get();
return 1;
}
I actually did not write any of this code so my own syntax errors cannot be an issue. Basically that means I'm out of ideas for troubleshooting. Any ideas as to why I can't run this?
Okay so saving the file as a .cpp worked for the building, but when my program actually runs nothing appears in the menu that pops up in which, I assume, the text is supposed to appear. Again, I'm decent at troubleshooting but this code has been confirmed to work by thousands of others and there must be something else wrong.
Save your file in .cpp format instead of .c format which is default for Code::Blocks. Your workspace(that is the file where you saved this code in) will be renamed to xyz.cpp and you can easily check this fact in the tab.Furthermore, change the cout and cin statements to std::cout and std::cin.
Just to make sure we are on the same page.Goto Settings>>>Compiler.Selected compiler should be GNU GCC compiler. Goto Toolchain Executables tab and autodetect the compiler's installation directory (should be something like CodeBlocks\MinGW).
Code::Blocks compiles using some built-in .dlls and i have sometimes found it needed the dll in the folder with the compliled .exe
if not that, try the console application template
i use TDM-GCC it compiles fine.

Eclipse g++ not found in path: windows

I am attempting to setup SDL2 for C++ with Eclipse on Windows 7.
In order to do so, I am following the tutorial in this link, which states that I must first install MinGW. So I follow the link provided in order to setup MinGW. I follow all the steps without issue. I then open Eclipse and attempt to build a simple hello world program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
To my surprise, this code doesn't build, with 6 errors.
I then proceed to simplify the program further:
int main()
{
return 0;
}
This also does not compile. There are two errors:
Program "g++" not found in PATH
Program "gcc" not found in PATH
Here is a screenshot.
However, my path does contain "C:\mingw\bin". I have also tried changing this to "C:\mingw". Looking inside "C:\mingw\bin", I find gcc and g++:
In addition, compiling a test program using the command line (g++ Test.cpp -o Test) works just fine, as does "g++ -v".
I have been searching the web for hours, and can't seem to find an answer as to why Eclipse can't seem to compile anything with MinGW. Questions I have looked at on SO (which haven't been able to fix my issue) include:
Eclipse mingw binary not found
Eclipse not finding c std libraries
g++ not found in path
Eclipse C++ : "Program g++ not found in PATH"
Program g++ not found in path
Program g++ not found in path C++ [duplicate]
Eclipse CDT (Juno) in Win7: Cannot find g++ in PATH, iostream unresolved and other fun stuff
Additional info:
Window > Preferences > C/C++ > Build > Settings > "CDT GCC Built-in Complier Settings MinGW [Shared]" : Toolchain MinGW GCC is not detected on this system.
I have also reinstalled Eclipse to no avail.
I realize that this may be a duplicate question of some that I have linked, but the information in previous questions have not been able to fix my problem, and I fear that adding a comment to an old question may not result in an answer.
Please request additional information as needed.
You need to set the environment for the c/c++ builder.
First you need to install the GNU tool-chain, you can choose either MinGW or Cygwin. You can see the steps here. I used MinGW.
Go to Window->Preferences->C/C++->Build->Environment and add a new variable, name it whatever you want for example a named it "MINGW", now paste the binaries directory of MinGW which is by default C:\MinGW\bin, you should have something like this:
Now when you create a new project you just have to select the MinGW tool-chain:
Hope that helps.
It appears as though I have fixed the problem for the moment.
In case others encounter the same issue:
Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Command changed from "g++" to "C:\mingw\bin\g++".

How do I use a shared dynamic library in c++ project?

I am running 64bit Linux and using Netbeans 8.0 IDE. I simply created new C++ project, so far only containing some HelloWorld code. I want to use a third party library for logging. I chose log4cpp (since I have experience with Java and it is supposed to have similar interface).
I installed liblog4cpp from the repositories, and can see it in /usr/lib, to be clear:
$ locate log4cpp
/usr/lib/liblog4cpp.so
/usr/lib/liblog4cpp.so.5
/usr/lib/liblog4cpp.so.5.0.5
/usr/lib/x86_64-linux-gnu/liblog4cpp.so
/usr/lib/x86_64-linux-gnu/liblog4cpp.so.5
/usr/share/doc/liblog4cpp5
/usr/share/doc/liblog4cpp5/AUTHORS
/usr/share/doc/liblog4cpp5/NEWS.gz
/usr/share/doc/liblog4cpp5/README
/usr/share/doc/liblog4cpp5/THANKS
/usr/share/doc/liblog4cpp5/TODO
/usr/share/doc/liblog4cpp5/changelog.Debian.gz
/usr/share/doc/liblog4cpp5/changelog.gz
/usr/share/doc/liblog4cpp5/copyright
/var/cache/apt/archives/liblog4cpp5_1.0-4_amd64.deb
/var/lib/dpkg/info/liblog4cpp5.list
/var/lib/dpkg/info/liblog4cpp5.md5sums
/var/lib/dpkg/info/liblog4cpp5.postinst
/var/lib/dpkg/info/liblog4cpp5.postrm
/var/lib/dpkg/info/liblog4cpp5.shlibs
I am having big trouble on how to #include it in my application. Inspired by example from the documentation I put in my source:
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
I was first using Makefile generated by netbeans, however could not figure out how to configure the project to make it work. All my trials lead to the following error (line 16 being the first #include):
$ make
src/main.cpp:16:31: fatal error: log4cpp/Category.hh: No such file or directory
compilation terminated.
make: *** [build/NetworkConfiguration.o] Error 1
I also tried a custom Makefile, giving g++ argument -llog4cpp, but I always get the same error. I can provide more info/code if it will be helpful. The solution does not need to depend on the IDE, I feel like I just misunderstood how linking works, so feel free to educate me on how to achieve what I want from console with custom Makefile for example.
Since cup still has not posted an answer, I am paraphrasing his comment as an accepted answer.
Install liblog4cpp-dev. The difference is that liblog4cpp only contains the libraries while liblog4cpp-dev is the development kit which includes the headers.