0

I'm still having issues with an ongoing project that just won't compile. I've narrowed it down to the Includes but can't figure out what is going on. I've read that i need to add a WinMain entry point but that doesnt add up - I have classmates that didnt encounter this shit error at all. So I've created a new empty project:

    #include <cstdlib> //include c library

    //using namespace std;
    //using namespace cv;

    namespace sp {
    int main() {
        return 0;
    }
    }

With the following includes:

Under GCC C++ Compiler Includes:

    C:\Users\Amit\Desktop\opencv\build\include
    C:\opencv_contrib-3.0.0\modules\xfeatures2d\include

Under MinGW C++ Linker Libraries:

    libopencv_core310
    libopencv_imgcodecs310
    libopencv_imgproc310
    libopencv_xfeatures2d310
    libopencv_features2d310
    libopencv_highgui310

Under MinGW C++ Linker Library search path:

    C:\Users\Amit\Desktop\opencv\build\x86\mingw\lib

Still, without calling any function from those libraries, I'm getting this error:

    09:45:43 **** Incremental Build of configuration Debug for project testing ****
    Info: Internal Builder is used for build
    g++ "-IC:\\opencv_contrib-3.0.0\\modules\\xfeatures2d\\include" "-IC:\\Users\\Amit\\Desktop\\opencv\\build\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\testing.o" "..\\src\\testing.cpp" 
    g++ "-LC:\\Users\\Amit\\Desktop\\opencv\\build\\x86\\mingw\\lib" -o testing.exe "src\\testing.o" -llibopencv_core310 -llibopencv_imgcodecs310 -llibopencv_imgproc310 -llibopencv_xfeatures2d310 -llibopencv_features2d310 -llibopencv_highgui310 
    c:/mingw/bin/../lib/gcc/mingw32/4.9.3/../../../libmingw32.a(main.o):(.text.startup+0xa7): undefined reference to `WinMain@16'
    collect2.exe: error: ld returned 1 exit status

    09:45:43 Build Finished (took 396ms)

Can anyone save me?

Thanks, Amit.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
doria90
  • 45
  • 8
  • 1
    Where do you define the entry point: *global* (not in namespace) `main()` function or `WinMain` function? – MikeCAT Aug 17 '16 at 07:17
  • 6
    `main()` must reside in the global namespace. – πάντα ῥεῖ Aug 17 '16 at 07:18
  • Main can't be in a namespace because it has to be global (for the OS to call it). – Spitzbueb Aug 17 '16 at 07:19
  • @Wernerson `main()` (not used as entry point) can be in a namespace because the name is not reserved ([N3337](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) 3.6.1 Main function, paragraph 3) – MikeCAT Aug 17 '16 at 07:20
  • If you don't want to create an entry point function, you should set the compiler to build a (static or dynamic) library. – MikeCAT Aug 17 '16 at 07:21

2 Answers2

2

When you create an executable the linker expects a function named main in the global namespace. You have placed the function inside a namespace instead of the global namespace so the linker will not find it.

So either move your main outside of the sp namespace or tell the linker where the function is (at least that is possible with MS linker but not sure how it is done with g++).

AndersK
  • 35,813
  • 6
  • 60
  • 86
2
namespace sp {
    int main() {
        return 0;
    }
}

declares an sp::main function, not main. This leaves you without a main function to serve as the program entry point.

Solution: Remove main from the sp namespace.

int main() {
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54