rand() in MinGW g++ - c++

I have a basic C++ file like so:
#include <iostream>
using namespace std;
int main() {
float x = rand();
cout << x << endl;
return 0;
}
When I run this through g++ on Ubuntu with g++ test.cpp -o test -std=c++11, I get no errors, and the program runs just fine. But when I run it through g++ on MinGW with the same command, I get the following error:
test.cpp: In function 'int main()':
test.cpp:6:17: error: 'rand' was not declared in this scope
float x = rand();
^
I have GCC version 5.3.0. Attempting to compile with g++ test.cpp -o test.exe -std=gnu++11 or g++ test.cpp -o test.exe -std=c++0x yield the same result.

You must include library for the random function first
i-e
#include < cstdlib >
After that your code will work perfectly
Here is the correct code
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
float x = rand();
cout << x << endl;
return 0;
}
Some compilers allow you to use random function without including library but standard C++ compilers doesn't allow you.
Hope this will help you

Related

How to use std::thread on Windows with MinGW with POSIX threads

I have been trying to compile and run the following very simple bit of code
#include <thread>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
I am compiling on Windows 10 with MinGW 6.3.0 (target x86_64-w64-mingw32) and the thread model (obtained using g++ -v) is POSIX.
I am not getting any compilation error with g++ -Wall -g test.cpp -o test.exe, however I am getting a runtime error when trying to run the exe (entry point of _ZNSt6thread15_M_start_threadESt10unique_ptrINS_3_StateESt14default_deleteIS1_EEPFvve cannot be found).
I also tried compiling with the -pthreador -lpthread flags, but I am getting the same runtime error.
Obviously this seems to be related to the use of std::thread, but I didn't get how to fix this. Am I missing a compilation flag or additional library enabling POSIX thread support on Windows?
Edit: I managed to get it working by changing the compilation command to
g++ -Wall -g test.cpp -o test.exe -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,Bdynamic
I tried your code after making the necessary fixes:
#include <thread>
#include <iostream>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
std::cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
and it worked fine.
The build command was the same as yours:
g++ -Wall -g test.cpp -o test.exe
The output was:
012345678910Press any key to continue . . .
But I did use MinGW-w64 GCC 12.1.0 (fro https://winlibs.com/) instead of the very old version 6.3.0.
Maybe the MinGW-w64 (or was it still the old MinGW) library you were using was simply too old...

g++ -std=c++17 gives lines and lines of errors

I am trying to compile this code with MinGW g++ (i686-win32-dwarf-rev0, Built by MinGW-W64 project) 8.1.0
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<int, int> mmap;
mmap[0]=10;
mmap[1]=20;
mmap[2]=30;
mmap[3]=40;
mmap[4]=50;
for(auto [x,y]:mmap){
cout<<x<<"->"<<y<<endl;
}
return 0;
}
Compiling with c++11 flag gives this
E:\Code>g++ temp.cpp -std=c++11
temp.cpp: In function 'int main()':
temp.cpp:89:14: warning: structured bindings only available with -std=c++17 or -std=gnu++17
for(auto [x,y]:mmap){
and compiling with c++17 flag gives lines and lines of errors.
g++ temp.cpp -std=c++17
OK, so I figured this out and this came out to be the very first line
#include <bits/stdc++.h>
Including iostream and map instead of the above line results in a clean compilation.
#include<iostream>
#include<map>
Now I have one more reason as to Why should I not #include <bits/stdc++.h>?

C++ gcc Namespace not found

I know there are many similar topics but there are equally many unique mistakes that may lead to this problem (so I think). Therefore I ask, after some research.
My problem is that the compiler, GNU GCC, when compiling one file does not see my namespace declared in another file. The IDE (CodeBlocks) evidently does see it as it auto-completes the name of the namespace. I tried to isolate the problem and came up with this:
File main.cpp:
namespace MyName
{
int MyVar;
}
#include "T1.cpp"
int main()
{
return 0;
}
File T1.cpp:
using namespace MyName;
error: 'MyName' is not a name-space name.
In my project I have a header file, say T1.h, and an implementation file T1.cpp — and MyName isn't accessible in either of them.
Any help or guidance would be appreciated.
What's happening is that CodeBlocks is compiling both main.cpp and T1.cpp. Here is what happens when you try to compile each one:
main.cpp:
$ g++ main.cpp
$
T1.cpp
$ g++ T1.cpp
T1.cpp:1:17: error: ‘MyName’ is not a namespace-name
using namespace MyName;
^
T1.cpp:1:23: error: expected namespace-name before ‘;’ token
using namespace MyName;
^
$
T1.cpp, when compiled on it's own, has no knowledge of MyName. To fix this, don't include .cpp files, and put your declarations in header files.
Edit: From what I gather, this may be a better way to organize your example:
T1.h:
namespace MyName {
extern int MyVar;
}
T1.cpp
#include "T1.h"
int MyName::MyVar = 5;
main.cpp
#include "T1.h"
#include <iostream>
using namespace MyName;
int main()
{
std::cout << MyVar << std::endl;
return 0;
}
Now it will compile correctly:
$ g++ -c T1.cpp -o T1.o
$ g++ -c main.cpp -o main.o
$ g++ T1.o main.o
$ ./a.out
5

How to link a program in C++

My code is:
test.cpp
#include<iostream>
#include<boost/bind.hpp>
#include "extern.h"
using namespace std;
using namespace boost;
int fun(int x,int y){return x+y;}
/*
*void add(int &m,int &n);
*/
int main(){
int m=1;int n=2;
cout << m << " "<< n << endl;
add(m,n);
cout << m << " "<< n << endl;
return 0;
}
extern.h:
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
void add(int &n,int &m);
extern.cpp:
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
extern int m;
extern int n;
void add(int &n,int &m) {
n = n+1;
m = m+1;
}
When I compile it with
g++ -Wall -o test test.cpp
It turns out to be:
/tmp/ccMHVRNo.o: In function `main':
test.cpp:(.text+0x7b): undefined reference to `add(int&, int&)'
collect2: ld returned 1 exit status
But when I compile it with:
g++ -Wall -o test test.cpp extern.cpp
It works well:
$ ./test
1 2
2 3
So the reason is that test.cpp can't find the implementation of the add() function.
But I have added extern.h to test.cpp, why does it still say "undefined reference to add(int&, int&)"?
The header file extern.h only tells your program how the prototype of the function is made. The linker needs the actual implementation of the function, so it looks for the code add(int&,int&) references to, and it cannot find it unless you give to the compiler all the files it needs (in this case, extern.cpp is the file the linker needs when looking for the add function).
The implementation of the function void add(int&,int&) is in the source file extern.cpp. The compiler can't know that this files is related to the program until you tell it.
You must specify all source files at the command line:
g++ -Wall -o test test.cpp extern.cpp
If you want to avoid compiling of all source files you can also specify the already compiled object file:
g++ -Wall -o test test.cpp extern.o
When you don't want to think every time what commands you need to compile you should use make and create an Makefile with rules that define how the target is build. with the makefile you can just start
make
and get the result.
This is the usual formula:
g++ -Wall -c test.cpp
g++ -Wall -c extern.cpp
g++ -o test test.o extern.o
There are one-liners, but those don't scale so well to larger projects.

g++ error: ‘malloc’ was not declared in this scope

I'm using g++ under Fedora to compile an openGL project, which has the line:
textureImage = (GLubyte**)malloc(sizeof(GLubyte*)*RESOURCE_LENGTH);
When compiling, g++ error says:
error: ‘malloc’ was not declared in this scope
Adding #include <cstdlib> doesn't fix the error.
My g++ version is: g++ (GCC) 4.4.5 20101112 (Red Hat 4.4.5-2)
You should use new in C++ code rather than malloc so it becomes new GLubyte*[RESOURCE_LENGTH] instead. When you #include <cstdlib> it will load malloc into namespace std, so refer to std::malloc (or #include <stdlib.h> instead).
You need an additional include. Add <stdlib.h> to your list of includes.
Reproduce this error in g++ on Fedora:
How to reproduce this error as simply as possible:
Put this code in main.c:
#include <stdio.h>
int main(){
int *foo;
foo = (int *) std::malloc(sizeof(int));
*foo = 50;
printf("%d", *foo);
}
Compile it, it returns a compile time error:
el#apollo:~$ g++ -o s main.c
main.c: In function ‘int main()’:
main.c:5:37: error: ‘malloc’ was not declared in this scope
foo = (int *) malloc(sizeof(int));
^
Fix it like this:
#include <stdio.h>
#include <cstdlib>
int main(){
int *foo;
foo = (int *) std::malloc(sizeof(int));
*foo = 50;
printf("%d", *foo);
free(foo);
}
Then it compiles and runs correctly:
el#apollo:~$ g++ -o s main.c
el#apollo:~$ ./s
50