Solved C++ <complex> yielding strange behavior
-
Hi -
I don't know whether this is a Qt issue or not, but I'm getting some weird behavior from Qt Creator. I created a new plain C++ project, and wrote the main routine thus:
#include <iostream> #include <complex> using namespace std; int main() { complex<double> a(2.0, 3.0); complex<double> b(4.0, 5.0); complex<double> c; c = a + b; cout << "a + b = " << c <<endl; c = a * b; cout << "a * b = " << c <<endl; c = b - a; cout << "b - a = " << c <<endl; c = b / a; cout << "b / a = " << c <<endl; return 0; }
Built and run, and it worked (I swear).
Renamed main.cpp to complex.cpp and got a bunch of build errors:
18:56:36: Running steps for project complex... 18:56:36: Configuration unchanged, skipping qmake step. 18:56:36: Starting: "/usr/bin/make" make: Circular complex.o <- complex dependency dropped. g++ -c -pipe -g -std=gnu++11 -Wall -W -fPIC -DQT_QML_DEBUG -I../complex -I. -I/opt/Qt/5.9/5.9.1/gcc_64/mkspecs/linux-g++ -o complex.o ../complex/complex.cpp In file included from ../complex/complex.cpp:2:0: ./complex:1:1: error: stray ‘\177’ in program
Renamed the file back to complex.cpp and now I get this:
/opt/Qt/5.9/5.9.1/gcc_64/bin/qmake -o Makefile ../complex/complex.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug rm -f main.o rm -f *~ core *.core 18:57:56: The process "/usr/bin/make" exited normally. 18:57:56: Configuration unchanged, skipping qmake step. 18:57:56: Starting: "/usr/bin/make" make: Circular main.o <- complex dependency dropped. g++ -c -pipe -g -std=gnu++11 -Wall -W -fPIC -DQT_QML_DEBUG -I../complex -I. -I/opt/Qt/5.9/5.9.1/gcc_64/mkspecs/linux-g++ -o main.o ../complex/main.cpp In file included from ../complex/main.cpp:2:0: ./complex:1:1: error: stray ‘\177’ in program ELF > `@ @ �{ @ 8 @ $ ! @ @ @ @ @ � � @ @ @
...and a lot more.
Running Creator 4.3.1 on Linux, using GCC. Ran the program on a Mac and got similar results (with CLANG).
I have no idea whatsoever what's going on. Anyone? Thanks.
-
make: Circular complex.o <- complex dependency dropped.
@mzimmers I don't fully understand what happened, but I'm guessing that
make
got confused by multiple things called "complex" in your project and its dependencies (#include <complex>)What happens if you delete your build folder, make sure your file is not called complex.cpp, and run qmake again?
-
Move your executable out of your source tree! ;)
#include <complex>
This includes your executable (as the current dir is first to be searched) instead of the standard header, thus you get the weird errors. Use a separate folder for building and you'll be fine.
-
Thanks for the replies, guys. The problem was indeed linked to a filename confusion. I renamed the project to "complex_test" and it worked just fine. You see something new every day...
kshegunov: I was using a shadow build, so I didn't expect the executable to be found. Is it temporarily in the source directory before the build finishes?
-
@mzimmers said in C++ <complex> yielding strange behavior:
Is it temporarily in the source directory before the build finishes?
No, but if your output (build) directory is in the include path then that'd do it.
-
@kshegunov Hmm...it's almost certainly not, at least not explicitly by anything I did. Perhaps some more experimentation is in order. I'm curious as to whether this would occur with other libraries.
-
@mzimmers said in C++ <complex> yielding strange behavior:
Hmm...it's almost certainly not, at least not explicitly by anything I did. Perhaps some more experimentation is in order. I'm curious as to whether this would occur with other libraries.
Do you use the widgets module? If you do try to determine the
.ui
file's output dir, this might be a reason why the build dir may be added to the include path. Can you share the project file? -
No widgets. Here's the project file:
TEMPLATE = app CONFIG += console c++11 CONFIG -= app_bundle CONFIG -= qt SOURCES += \ main.cpp
Weird, huh? I'm not sure whether it's the .cpp file or the project file that's getting in the way, but we're definitely on the right track.
-
@mzimmers said in C++ <complex> yielding strange behavior:
Weird, huh?
Indeed. What about the compile and link line? There should be some clue somewhere why this is being included.
-
Here's the compile line, and a little output:
09:40:59: Starting: "/usr/bin/make" g++ -c -pipe -g -std=gnu++11 -Wall -W -fPIC -DQT_QML_DEBUG -I../complex_test -I. -I/opt/Qt/5.9/5.9.1/gcc_64/mkspecs/linux-g++ -o main.o ../complex_test/main.cpp In file included from ../complex_test/main.cpp:2:0: ./complex:1:1: error: stray ‘\177’ in program
Another data point: I tried it with the source file named "main.cpp" but the project file still "complex.pro" and it failed.
-
@mzimmers said in C++ <complex> yielding strange behavior:
g++ ... -I.
This is what does it. I'm not sure where it comes from though. As a workaround rename your executable, so it doesn't conflict:
TARGET = complex_app
-
OK, that makes sense. I imagine this same problem would occur with other libraries as well. Good lesson learned. Thanks for the help.
PS: I wonder whether this would happen on Windows. I'd expect the .exe suffix to prevent it, but who knows...
-
@mzimmers said in C++ <complex> yielding strange behavior:
I'd expect the .exe suffix to prevent it, but who knows...
Yes, that is correct, it's a linux/mac peculiarity.