Search code examples
c++gccopenmp

Error "undefined reference to omp_get_wtime"


I cannot find which library to link in GCC (4.8) under Windows (Vista). I tried the -fopenmp -llibgomp -lgomp compiler directives, but none works.

I already have GCC with POSIX (so std::thread is working if enabling C++11). The problem seems that searching for the right library does not provide useful results (even searching in GCC/MinGW documentation).

So basically I can't get this answer working (the answer claimed to work on most compilers, but it doesn’t provide additional information on how to get it working, so I can't verify if it is really working or not).

It would be nice to provide now additional information to get it working on most systems...


Solution

  • MinGW-w64 based on GCC 4.8.1 from here has no problems so far.

    Example: C


    main.c

    #include <omp.h>
    #include <stdio.h>
    
    int
    main() {
      double x = omp_get_wtime();
    
      printf("%f\n", x);
    }
    

    Build:

    gcc main.c -lgomp -o test.exe
    

    Result:

    1381572544.299000
    

    Example: C++


    main.cpp

    #include <iostream>
    #include <omp.h>
    
    using std::cout;
    using std::endl;
    
    int
    main() {
      double x = omp_get_wtime();
    
      cout << x << endl;
    }
    

    Build:

    g++ main.cpp -lgomp -o test.exe
    

    Result:

    1.38158e+009
    

    Conclusion


    Probably something is wrong with your MinGW distribution. Otherwise I don't see any reason for it not to work. Try the above one and see how it goes.