Making a simple hello world app in c++, but it won't compile. I have the folder C:\WiiGames\e3\ with the files main.cpp and Makefile. My makefile is:
build: main.cpp
C:/MinGW/bin/g++.exe main.cpp -o e3.exe
My error is:
C:\WiiGames\e3>make build
C:/MinGW/bin/g++.exe main.cpp -o e3.exe
make: *** [build] Error 1
C:\WiiGames\e3>
Any help would be greatly appreciated.
My code:
#include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>
#include <cstdlib>
#include <time.h>
int main() {
printf("Hello World!");
}
g++ -v:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.6.2 (GCC)
Make sure it is a tab rather than spaces in the make file before the c:/mingw... Line. It looks like it is make giving the error, not g++.
What happens when you run g++.exe main.cpp -o e3.exe on the command line directly? If nothing happens, is g++ in your path? I'd start by making sure that g++ is setup and in your path.
Yes, it's an old question. These are some solutions i came across (YMMV a lot):
Check if your paths start with c:/ as opposed to /c/ or vice-versa.
Check if you're running in the right shell, i.e., one of:
c:\msys64\msys2_shell.cmd -mingw32
c:\msys64\mingw32.exe
c:\msys64\usr\bin\bash.exe
which may differ if used within an IDE.
Related
I tried to inteface between C++ and D.
The problem is that I can't compile it. I used the code/command line on the website
> g++ -c foo.cpp
> dmd bar.d foo.o -L-lstdc++ && ./bar
and it didn't work: first of all the DMD compiler doesn't use .o files so i had to specify manually to create a .obj file then the linker threw an error because it didn't knew /lstdc++ so i configured the cpp libs manually.
Thats the point where i am now it throws an "Symbol not found" error and i don't know how to fix this, am i doing something wrong on compiling? or is it on the programming site?
here my compile commands:
PS C:\Users\kunde\Downloads\d-interface test> g++ -c -o test-cpp.obj test.cpp
PS C:\Users\kunde\Downloads\d-interface test> dmd -L"G:\GNUstep\lib\gcc\mingw32\4.6.1\libstdc++.dll.a" -L"G:\GNUstep\lib\gcc\mingw32\4.6.1\libstdc++.a" .\test-cpp.obj .\test.d
test.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""char __cdecl foo(int)" (?foo##YADH#Z)" in Funktion "__Dmain".
test-cpp.exe : fatal error LNK1120: 1 nicht aufgelöste Externe
Error: linker exited with status 1120
and my code:
test.d:
import std.stdio;
extern (C++) char foo(int i);
void main(string[] args)
{
writefln("%c", foo(14));
}
test.cpp:
#include <iostream>
extern "C" {
#include "test.h"
}
char foo(int i){
std::cout << "got: ";
return (char)bar(i);
}
and test.h:
#include <stdio.h>
int bar(int a){
printf("returned %d", a*a);
return a*a;
}
What am I doing wrong?
P.s.: im german so the errors may have to be translated
Edit: As sayd in the answers MSVC fixed the problem i changed "g++ -c -o test-cpp.obj test.cpp" to "cl test.cpp -c /EHsc"
C++ is a finicky beast - its name mangling details are implementation defined, so it varies by platform and compiler. dmd picks one it considers the main compiler on the system and copies it. On Linux, that's g++.... but on Windows, it is Microsoft C++, not g++.
So you have to use a matching compiler. On Windows, there is the gdc compiler - a D compiler built into gcc - but there's no binary builds for it available at this time. You'd have to compile it yourself with some config flags...
../gcc/configure --prefix=/mingw64 --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-bootstrap --disable-werror --disable-libssp --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libitm --disable-libsanitizer --disable-libstdcxx-pch --with-arch=x86-64 --with-gnu-as --with-gnu-ld --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-languages=d --enable-libphobos --enable-shared --enable-static --disable-multilib
is what I used.
It is a bit of a pain. I've never actually tested the extern(C++) on gdc/Windows either.
So I'd suggest either using the MSVC compiler on the C++ together with dmd.... or probably better yet making the interface extern(C) instead. The C interface is far more compatible across things.
And the .o vs .obj thing is the same case - gcc uses .o, dmd and msvc use .obj (at least -m64 bit builds match, 32 bit is more complicated depending on version number)... so really using the same compiler family is going to get you best results.
I have been starting to try and learn how to utilize multithreading in c++, but the #include<thread> causes issues with the thread type being not declared in the scope. error: 'thread' was not declared in this scope. I have been doing research, and I have come across a lot of answers regarding how to solve this issue. Im presently at the understanding that my compiler, MinGW, doesn't support thread effectively, but I am not sure what to do with that information.
Any guidance on this matter is appreciated.
Also, I think this may be helpful. If I run gcc -v on my command line, I get this output:
Using built-in specs.
COLLECT_GCC=c:\MinGW\bin\gcc.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
edit: I've seen this webpage, https://github.com/meganz/mingw-std-threads, as a potential solution, but I don't think this works for me. Unless somehow I am putting the mingw-thread.h in the wrong folder.
I think the problem here is you did not tell the compiler using c++11 features. 'thread' is belonged to c++11 features, let try adding -std=c++11 into CXXFLAGS or CPPFLAGS and see if it resolves your problem
My psychic powers suggest your forgot the std:: namespace attribute. That would explain why thread is undefined, even with #include <thread>. The other answer about -std=c++11 is also steering you into the right direction. And don't forget the -pthread compiler/linker option.
$ cat foo.cpp
#include <thread>
#include <iostream>
void threadfunc()
{
std::cout << "Hello from the worker thread" << std::endl;
}
int main()
{
std::thread t(threadfunc);
t.join();
return 0;
}
$ g++ foo.cpp -std=c++11 -o foo -pthread
$ ./foo
Hello from the worker thread
I have some C++ code that compiles nicely on Linux but so far I'm having trouble getting it to compile correctly on NetBSD.
These are my includes:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <iomanip>
#include <boost/regex.hpp>
I compile with this command:
g++ -v -O2 -fno-strict-aliasing -I /usr/pkg/include \
barefoot.cpp -o barefoot -L /usr/pkg/lib/ -lboost_regex \
-L /usr/pkg/lib/ -lboost_regex -lpthreads
Which renders this output:
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64--netbsd
Configured with: /usr/src2/tools/gcc/../../external/gpl3/gcc/dist/configure --target=x86_64--netbsd --enable-long-long --enable-threads --with-bugurl=http://www.NetBSD.org/Misc/send-pr.html --with-pkgversion='NetBSD nb2 20111202' --enable-__cxa_atexit --with-tune=nocona --with-mpc=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-mpfr=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-gmp=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --enable-tls --disable-multilib --disable-symvers --disable-libstdcxx-pch --build=x86_64-unknown-netbsd5.99.56 --host=x86_64--netbsd
Thread model: posix
gcc version 4.5.3 (NetBSD nb2 20110806)
COLLECT_GCC_OPTIONS='-v' '-O2' '-fno-strict-aliasing' '-I' '/usr/pkg/include/' '-o' 'a.out' '-L' '/usr/pkg/lib/' '-mtune=nocona' '-march=x86-64'
/usr/libexec/cc1plus -quiet -v -I /usr/pkg/include/ barefoot.cpp -quiet -dumpbase barefoot.cpp -mtune=nocona -march=x86-64 -auxbase barefoot -O2 -version -fno-strict-aliasing -o /var/tmp//cc9Dcmwi.s
GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
#include "..." search starts here:
#include <...> search starts here:
/usr/pkg/include/
/usr/include/g++
/usr/include/g++/backward
/usr/include/gcc-4.5
/usr/include
End of search list.
GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
Compiler executable checksum: a34e7d170f4dd8d4687d2b62e8dca4b7
In file included from /usr/include/g++/bits/gthr.h:166:0,
from /usr/include/g++/ext/atomicity.h:34,
from /usr/include/g++/bits/ios_base.h:41,
from /usr/include/g++/ios:43,
from /usr/include/g++/istream:40,
from /usr/include/g++/fstream:40,
from barefoot.cpp:29:
/usr/include/g++/bits/gthr-default.h:130:9: error: 'pthread_detach' was not declared in this scope
/usr/include/g++/bits/gthr-default.h:130:1: error: invalid type in declaration before ';' token
/usr/include/g++/bits/gthr-default.h: In function 'int __gthread_detach(pthread_st*)':
/usr/include/g++/bits/gthr-default.h:668:46: error: '__gthrw_pthread_detach' cannot be used as a function
In file included from /usr/pkg/include/boost/regex/v4/regex_workaround.hpp:25:0,
from /usr/pkg/include/boost/regex/v4/regex.hpp:32,
from /usr/pkg/include/boost/regex.hpp:31,
from barefoot.cpp:32:
/usr/include/g++/cstdlib: At global scope:
/usr/include/g++/cstdlib:132:11: error: '::system' has not been declared
I tried adding -lpthread, but that resulted in the same output. Not sure what to try next.
I do not know exactly why you are facing this problem, but I can provide you with some next steps to try.
Notice that the first error is that pthread_detach is missing. The system header files will typically make sure whatever headers they need are included, but you can try adding <pthread.h> above all the other headers.
If that fails, you need to figure out which file got included as a result of adding #include <pthread.h>, and make sure pthread_detach is present inside it (it really should be there somewhere).
Assuming it is there, there must be some conditional compilation that is causing it to not be visible to your source code. Find the conditional guards, and what macro values affect it.
Then, you will need to see why your system is defining the macro values in a way that causes the function pthread_detach to not be visible.
With this investigation, you may discover that you are not including the right <pthread.h> file. This may be due to system include directories conflicting with the include directories you want to use.
This answer (it is towards the bottom) to this question shows how to display the predefined macros for the g++ compiler.
Good morning,
I am a new user of Windows (+10 years using Linux) and for some reason I have to compile a C++ program. I do not have a big experience in debugging in Windows, but I am eager to understand what I do wrong.
I use MinGW with g++ and different libraries, particularly one called libjpeg.
When I compile & build the file in a PowerShell using the following command, everything works fine, and my C++ code gives me the expected results.
g++ -o imageProcessing imageProcessing.cpp -lgdi32 -Dgmic_use_jpeg -I'C:\GnuWin32\include' -L'C:\GnuWin32\lib' -ljpeg
When I use the exact same command in a .bat file, or directly in my C++ editor (geany), I have this error:
imageProcessing.cpp:6:21: fatal error: jpeglib.h: No such file or directory
#include <jpeglib.h>
^
compilation terminated.
It doesn't find the jpeglib.h.
The correct path were set in the environmental variables.
The libjpeg I use allows me to open/save jpeg pictures when I use the CImg library. It can be found here:
Jpeg for Windows
Here is a very simple example:
#include <iostream>
#include <jpeglib.h>
using namespace std;
int main() {
float number;
cout << "Enter a number: ";
cin >> number;
}
Let's call this test.cpp and its location is: C:\Users\Rémi\Documents\Image_Procesing\C++\testCImg
Here is what I've added in my environmental variables path: C:\GnuWin32\lib;C:\GnuWin32\bin;C:\MinGW\bin;
Here is my test.bat: g++ -o test test.cpp -lgdi32 -Dgmic_use_jpeg -I'C:\GnuWin32\include' -L'C:\GnuWin32\lib' -ljpeg
test.bat is also located in: C:\Users\Rémi\Documents\Image_Procesing\C++\testCImg
the .bat fail is the following:
C:\Users\Rémi\Documents\Image_Procesing\C++\testCImg>g++ -o test test.cpp -lgdi32 -Dgmic_use_jpeg -I'C:\GnuWin32\include
' -L'C:\GnuWin32\lib' -ljpeg
test.cpp:3:21: fatal error: jpeglib.h: No such file or directory
#include <jpeglib.h>
^
compilation terminated.
I spent all my day yesterday trying to figure it out, and I am limited by my Windows knowledge.
If someone could help me, I would appreciate it very much.
Thank you.
When you have include path problems, ask GCC to tell you what it's using.
In the below example, you can clearly see that it's searching for a location 'test' and ignoring it for not existing.
You can then deduce that, as mentioned elsewhere, the single quotes are wrong in Batch. At that point you'd give "test" a go and achieve success.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\lightness>g++ -E -x c++ - -v -I'test'
Using built-in specs.
COLLECT_GCC=g++
Target: mingw32
Configured with: ../../src/gcc-4.7.1/configure --build=mingw32 --enable-language
s=c,c++,ada,fortran,objc,obj-c++ --enable-threads=win32 --enable-libgomp --enabl
e-lto --enable-fully-dynamic-string --enable-libstdcxx-debug --enable-version-sp
ecific-runtime-libs --with-gnu-ld --disable-nls --disable-win32-registry --disab
le-symvers --disable-build-poststage1-with-cxx --disable-werror --prefix=/mingw3
2tdm --with-local-prefix=/mingw32tdm --enable-cxx-flags='-fno-function-sections
-fno-data-sections' --with-pkgversion=tdm-1 --enable-sjlj-exceptions --with-bugu
rl=http://tdm-gcc.tdragon.net/bugs
Thread model: win32
gcc version 4.7.1 (tdm-1)
COLLECT_GCC_OPTIONS='-E' '-v' '-I' ''\''test'\''' '-mtune=i386' '-march=i386'
c:/mingw32/bin/../libexec/gcc/mingw32/4.7.1/cc1plus.exe -E -quiet -v -I 'test'
-iprefix c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/ - -mtune=i386 -march=i386
ignoring nonexistent directory "c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/../../..
/../mingw32/include"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude/c++"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude/c++/mingw32"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude/c++/backward"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/../
../../../include"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude-fixed"
ignoring nonexistent directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/.
./../../../mingw32/include"
ignoring nonexistent directory "'test'"
#include "..." search starts here:
#include <...> search starts here:
c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include/c++
c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include/c++/mingw32
c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include/c++/backward
c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include
c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/../../../../include
c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include-fixed
End of search list.
# 1 "<stdin>"
Argument tokens in Windows traditionally use the " character. I'm thinking the single quotes are being interpreted as part of the path names, resulting in the "no such file or directory" errors. That would explain why it works in PowerShell, though, as single quotes are valid there.
See if you have better luck with this:
g++ -o imageProcessing imageProcessing.cpp -lgdi32 -Dgmic_use_jpeg -I"C:\GnuWin32\include" -L"C:\GnuWin32\lib" -ljpeg
I realize that there are several duplicates like this but none of them have worked for me so far.
I am trying to compile a C++ very simple program on Ubuntu using g++ but it is giving me scope errors.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
This gives me this error:
sudo g++ -v test.c
test.c: In function ‘int main()’:
test.c:7:3: error: ‘cout’ was not declared in this scope
I also tried defining the scope as many other posts say, but that also didn't work, but gave me a different error:
#include <iostream>
int main()
{
std::cout << "Hello world";
}
Gives error:
test.c: In function ‘int main()’:
test.c:6:3: error: ‘cout’ is not a member of ‘std’
Most of the suggestions online suggest "using namespace std;", "#include " and "std::cout".
So I tried all 3 together, still no luck :(
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello world";
}
gives error:
test.c: In function ‘int main()’:
test.c:7:3: error: ‘cout’ is not a member of ‘std’
I have gone through several forums online but none of them seem to work for me :(
This is a part of a bigger issue because of which one of my linux make doesn't work.
Btw, I am using g++ and not gcc as a few posts messed up.
EDIT 1:
I changed the name to .cpp, and execute without sudo. Here is the verbose output:
pranoy#pranoyubuntu1210:~/Desktop/SIP/SIPp/sipp-3.3$ g++ -v test.cpp -o test
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-2ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)
COLLECT_GCC_OPTIONS='-v' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.7/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -version -fstack-protector -o /tmp/cczzibvL.s
GNU C++ (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu)
compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.7
/usr/include/c++/4.7/x86_64-linux-gnu
/usr/include/c++/4.7/backward
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
GNU C++ (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu)
compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 521527ea42f0901bf839bcaad0cb13e6
test.cpp: In function ‘int main()’:
test.cpp:5:3: error: ‘cout’ is not a member of ‘std’
This isn't the case for this, but could be an answer to "error: ‘cout’ is not a member of ‘std’". I came here looking for help, so this might help someone else.
If you have a header file (header.h) where the last function/class declaration does not have a semi-colon after it:
#include <string>
function(std::string str)
And your c++ file includes this before iostream:
#include "header.h"
#include <iostream>
The function/class (function in this case) messes up the declarations in iostream. #include basically just pastes the text into the code. Adding the semi-colon can fix the problem.
What you've written works absolutely fine on my Ubuntu system, with the same version of g++.
It sounds like you haven't installed all of the necessary files for the C++ environment, or something isn't quite right with it. Try this:
$ sudo apt-get remove g++ libstdc++-6.4.7-dev
$ sudo apt-get install build-essential g++-multilib
(Run dpkg -l | grep libstdc++ to get the exact version of libstdc++ if the above fails)
For C++ programs, use g++, not gcc. If you get errors that suggest your compiler cannot find the standard library, that's because you probably used gcc.
(C++, Linux Terminal, no compiling error, but no prints into the Terminal), maybe try:
$ g++ yourcode.cpp
$ ./a.out
first line: g++ compiles your code.
second line: runs your compiled code.
Rename file extension with .cpp .
Compile source code by command
gcc source.cpp -o output.out
GCC will automatically compile it as a C++ program.