Undefined reference to timeGetTime() in g++ [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I'm using g++ command line under Windows 10 to build a basic timing loop and am getting the error: " undefined reference to `timeGetTime#0' " when attempting to compile.
The code, itself, is pretty simple:
#include <windows.h>
#include <iostream>
using namespace std;
int main(){
int start = timeGetTime();
int finish = 10000;
int benchmarks [9] = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000};
int i = 0;
int time = timeGetTime() - start;
while( time < finish){
if(time > benchmarks[i]){
cout << benchmarks[i] / 1000 << endl;
i++;
}
return 0;
}
Not sure what I need to do to get g++ to play nicely with the WinAPI. I can't help but wonder if it's an issue with the linker.

You have to link libwinmm.
Undefined reference is an error which occures when your compiler knows that the function exist, it knows its prototype, but can't find its corpse. When it is not one of your functions, it surely means you miss to link a library.

Related

Following c++ code works in leetcode but not in my vscode, why? [duplicate]

This question already has an answer here:
G++ Compiler warning when using c++ 17 updates
(1 answer)
Closed 1 year ago.
I am currently working on a leetcode question, and try to track down the code process in my end, this is my solution:
#include <iostream>
#include <vector>
#include <stack>
#include <utility>
using namespace std;
vector<int> direction{-1, 0, 1, 0, -1};
int maxAreaOfIsland(vector<vector<int>>&grid){
int m = grid.size(), n = m ? grid[0].size() : 0, local_area, area = 0, x, y;
for (int i = 0; i < m; ++i){
for (int j = 0; j < n; ++j){
if(grid[i][j]){
local_area = 1;
grid[i][j] = 0;
stack<pair<int, int>> island;
island.push({i, j});
while(!island.empty()){
auto [r, c] = island.top(); \\problem line, vscode can't understand it
island.pop();
for (int k = 0; k < 4; ++k){
x = r + direction[k], y = c + direction[k + 1];
if(x>=0 && x<m && y>=0 && y<n && grid[x][y]==1){
grid[x][y] = 0;
++local_area;
island.push({x, y});
}
}
}
area = max(area, local_area);
}
}
}
return area;
}
this code works on the leetcode side, but not mine, here is the warning
[Running] cd "c:\Users\chen1\OneDrive\Desktop\C_C++tut\" && g++ leetcode695.cpp -o leetcode695 && "c:\Users\chen1\OneDrive\Desktop\C_C++tut\"leetcode695
leetcode695.cpp: In function 'int maxAreaOfIsland(std::vector<std::vector<int> >&)':
leetcode695.cpp:23:16: warning: structured bindings only available with -std=c++17 or -std=gnu++17
auto [r, c] = island.top();
^
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
[Done] exited with code=1 in 0.932 seconds
Can someone explains why, although I get an alternative way to replace it, it is still annoying and perplexing
thanks for helping
additionally!!!
I actually have my main function; the problem here is a syntax error where leetcode's compiler recognizes it, but not g++, the line that causes the problem is auto [r, c] = island.top();, if I alter it to
int r = get<0>(island.top());
int c = get<1>(island.top());
then it works fine, I just don't understand why leetcode compiler can understand it, but not g++
The linker error (not the warning) is what is causing the build to fail (scroll sideways!):
[...]crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
occurs in MinGW gcc when your code lacks either a main() or WinMain() entry point. As yours does. I guess leetcode (which I have never heard of or used) provides a test harness for you to run the function? It compiles - the message is a linker error, so it cannot form an executable - you need a main().
With respect to the warning, again you need to scroll to the end of the message:
leetcode695.cpp:23:16: warning: structured bindings only available
with -std=c++17 or -std=gnu++17
auto [r, c] = island.top();
can be resolved by specifying C++17 (or higher) compilation (or not using structured bindings). A Windows/VSCode specific solution is discussed at G++ Compiler warning when using c++ 17 updates, but fundamentally it is about setting the compiler switch -std=c++17.

Why my Sleep time is way off while others' (Urho3D's) is accurate [duplicate]

This question already has answers here:
std::this_thread::sleep_for sleeps for too long
(1 answer)
Is Sleep() inaccurate?
(3 answers)
Closed 1 year ago.
I'm using "sleep" to achieve some sort of frame time control, but the result I've got so far is way off.
#include <iostream>
#include <chrono>
#include <thread>
#include <Windows.h>
int main(int argc, char** argv) {
for (int i = 0; i < 100; i++) {
auto old = std::chrono::system_clock::now();
//std::this_thread::sleep_for(std::chrono::milliseconds(33));
::Sleep(33u);
auto now = std::chrono::system_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(now - old).count() << std::endl;
}
return 0;
}
This code (either with ::Sleep or std sleep) prints an average of above 45 on my PC(Win10). I have no idea what I missed. Is it connected to some secret thread/process settings?
*When compiled and run in WSL, it's accurate, weird.
I looked into Urho3D's code. It looks nothing fancier than mine, but is magically accurate to milliseconds.
// Sleep if 1 ms or more off the frame limiting goal
if (targetMax - elapsed >= 1000LL)
{
auto sleepTime = (unsigned)((targetMax - elapsed) / 1000LL);
Time::Sleep(sleepTime); // calls ::Sleep on Windows
}
Thanks to Alan for pointing out that the second associated question's top answer solves the problem. Apparently I have to call timeBeginPeriod on Windows to make this work.

Intel OneAPI FFT Segmentation Fault and Bus Error [duplicate]

This question already has answers here:
Segmentation fault on large array sizes
(7 answers)
Closed 1 year ago.
I have some data of Complex Values of size N, and would like to compute the FFT of this data using the Intel OneAPI. Here is my code:
# Connectivity
#include <bits/stdc++.h>
#include "mkl_dfti.h"
#include <complex.h>
using namespace std;
float pi = 2*acos(0.0);
int main(){
long long int N; cin >> N
float _Complex c2c_data[N];
DFTI_DESCRIPTOR_HANDLE my_desc1_handle = NULL;
DFTI_DESCRIPTOR_HANDLE my_desc2_handle = NULL;
MKL_LONG status;
// data is inserted here
status = DftiCreateDescriptor(&my_desc1_handle, DFTI_SINGLE, DFTI_COMPLEX, 1, N);
status = DftiCommitDescriptor(my_desc1_handle);
status = DftiComputeForward(my_desc1_handle, c2c_data);
status = DftiFreeDescriptor(&my_desc1_handle);
cout << round(cabs(c2c_data[s]) / N) << "\n";
return 0;
}
This works for smaller cases of N, but for larger cases (around 2^21), I get a segmentation fault and for even larger cases, I get a Bus error. I have checked that this happens at the DftiComputeForward function. The length of the data is specified in DftiCreateDescriptor which is indeed N in this case, so I am not sure why I am getting this error.
Here is here how I compile my code:
dpcpp test.cpp -lmkl_intel_lp64 -lmkl_core -lmkl_intel_thread -liomp5 -ldl -lpthread -o test
It would be great if someone could help. Thank you!
you might be exhausting your stack memory space.
Please consider using a vector instead.

How is gcc optimizing this loop?

So I encountered some strange behavior, which I stripped down to the following minimal example:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vec;
for(int i = 0; i < 1000; i++)
{
vec.push_back(2150000 * i);
if(i % 100 == 0) std::cout << i << std::endl;
}
}
When compiling with gcc 7.3.0 using the command
c++ -Wall -O2 program.cpp -o program
I get no warnings. Running the program produces the following output:
0
100
200
300
400
500
600
700
800
900
1000
1100
1200
1300
[ snip several thousand lines of output ]
1073741600
1073741700
1073741800
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
which I guess means that I finally ran out of memory for the vector.
Clearly something is wrong here. I guess this has something to do with the fact that 2150000 * 1000 is slightly larger than 2^31, but it's not quite as simple as that -- if I decrease this number to 2149000 then the program behaves as expected:
0
100
200
300
400
500
600
700
800
900
The cout isn't necessary to reproduce this behavior, so I suppose a minimal example is actually
#include <vector>
int main()
{
std::vector<int> vec;
for(int i = 0; i < 1000; i++)
{
vec.push_back(2150000 * i);
}
}
Running this causes the program to wait for a long time and then crash.
Question
I'm fairly new to C++ at any serious level. Am I doing something stupid here that allows for undefined behavior, and if so, what? Or is this a bug in gcc?
I did try to Google this, but I don't really know what to Google.
Addendum
I see that (signed) integer overflow is undefined behavior in C++. To my understanding, that would only mean that the behavior of the expression
21500000 * i
is undefined -- i.e. that it could evaluate to an arbitrary number. That said, we can see that this expression is at least not changing the value of i.
To answer my own question, after examining the assembler output it looks like g++ optimizes this loop by changing
for(int i = 0; i < 1000; i++)
{
vec.push_back(2150000 * i);
}
to something like
for(int j = 0; j < 1000 * 2150000; j += 2150000)
{
vec.push_back(j);
}
I guess the addition is faster than doing a multiplication each cycle, and the rule about overflows being undefined behavior means that this change can be made without worrying about whether this introduces unexpected behavior if that calculation overflows.
Of course the conditional in the optimized loop always fails, so ultimately I end up with something more like
for(int j = 0; true; j += 2150000)
{
vec.push_back(j);
}

How can I find the exact rand() used in a C library? [duplicate]

This question already has answers here:
glibc rand function implementation
(2 answers)
Closed 6 years ago.
As a part of my coursework I need to find and re-code a rand() random number generator which outputs the same numbers as the original. The starting sequence is 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421 1025202362 and can be generated at http://ideone.com/H7tsSI
#include <stdlib.h> /* rand */
#include <iostream>
using namespace std;
int main ()
{
for (int i = 0 ; i< 10 ; i++) {
cout << rand() << " ";
}
cout << rand();
return 0;
}
My issue is that I can't find the original source of this generator, and I don't know how I can figure out the way the generator works from the full sequence of the generator, which is 100 numbers long. Could someone help me either find the original generator or teach me how I can find a generator from its sequence? Thanks!
Depending on your specific compiler you may have the source code available. On Visual Studio 12.0, for example, the rand() source code is:
int __cdecl rand (
void
)
{
_ptiddata ptd = _getptd();
return( ((ptd->_holdrand = ptd->_holdrand * 214013L
+ 2531011L) >> 16) & 0x7fff );
}
If your compiler does not include the source code for its C library, you could try using a disassembler to piece together what its version of the rand() function does. In general, most of them would be along the same lines of the above code: access a state variable that was the result of the last call to rand() (or the seed if it's the first call), perform a permutation on it, and then write that back to the state variable.
You can find the source library implementation used by GNU at http://www.gnu.org/software/libc/
If you're familiar with GIT, you can use the GIT source code management like :
$> git clone git://sourceware.org/git/glibc.git
rand() function essentially calling different functions - first __random which in turn calls __random_r . Click on the function names to refer to the source repository at version 2.15
For more details, refer to answers here - gcc implementation of rand()