简体   繁体   中英

Simple project won't compile

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.

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++).

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM