简体   繁体   中英

C++ Linux to Windows Cross-compilation error

I am trying to compile a C++ program for windows in linux. This program uses the SHGetKnownFolderPath function. Whenever I try to compile this, I get the following error:

$ x86_64-w64-mingw32-g++ main.cpp main.h -mwindows -o main.exe
/tmp/cc7yIaVK.o:main.cpp:(.text+0x11c): undefined reference to `SHGetKnownFolderPath'
/tmp/cc7yIaVK.o:main.cpp:(.rdata$.refptr.FOLDERID_Startup[.refptr.FOLDERID_Startup]+0x0): undefined reference to `FOLDERID_Startup'
collect2: error: ld returned 1 exit status

Here is the code that I wrote:

int WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd){
    //get startup folder
    if(SHGetKnownFolderPath(FOLDERID_Startup, 0, NULL, &startupFolder) != S_OK){
        //error in getting startup folder
        return -1;
    }
  /*
  ...
  */
}

I included the proper header files and defined the windows version:

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
#include <shlobj.h>

EDIT: I learned that I have to link a lib file in the compilation, presumably with the -l flag after the source file names, according to mingw.org documentation. I don't know what the name of the file I need is though.

I'm not sure what else to do.

Thanks for any help.

A quick find/nm (with a suitable nm that recognizes Windows binaries)0 gives:

rich@dev:~/ellcc-release/libecc/mingw/x86_64-w64-mingw32$ find . -name "*.a" | xargs ~/ellcc/bin/ecc-nm -A | grep FOLDERID_Startup
/home/rich/ellcc/bin/ecc-nm: ./sys-root/mingw/lib/libruntimeobject.a: File format not recognized
./sys-root/mingw/lib/libuuid.a:lib64_libuuid_a-uuid.o:0000000000000000 r .rdata$FOLDERID_Startup
./sys-root/mingw/lib/libuuid.a:lib64_libuuid_a-uuid.o:0000000000000000 R FOLDERID_Startup
rich@dev:~/ellcc-release/libecc/mingw/x86_64-w64-mingw32$

It looks as if -luuid is what you need.

I made a little test program:

rich@dev:~$ cat win.c 
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
#include <shlobj.h>
int WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd){
    const char *startupFolder;
    //get startup folder
    if(SHGetKnownFolderPath(&FOLDERID_Startup, 0, NULL, &startupFolder) != S_OK){
        //error in getting startup folder
        return -1;
    }
  /*
  ...
  */
}

rich@dev:~$ ~/ellcc/bin/ecc -target x86_64-w64-mingw32 win.c -luuid
win.c:7:57: warning: incompatible pointer types passing 'const char **' to
      parameter of type 'PWSTR *' (aka 'unsigned short **')
      [-Wincompatible-pointer-types]
    if(SHGetKnownFolderPath(&FOLDERID_Startup, 0, NULL, &startupFolder) != S...
                                                        ^~~~~~~~~~~~~~
/home/rich/ellcc/bin/../libecc/mingw/include/shlobj.h:746:92: note: passing
      argument to parameter 'ppszPath' here
  ...(REFKNOWNFOLDERID rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
                                                                  ^
1 warning generated.
rich@dev:~$

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