简体   繁体   English

C ++ Linux到Windows的交叉编译错误

[英]C++ Linux to Windows Cross-compilation error

I am trying to compile a C++ program for windows in linux. 我正在尝试为Linux中的Windows编译C ++程序。 This program uses the SHGetKnownFolderPath function. 该程序使用SHGetKnownFolderPath函数。 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: 我包括适当的头文件并定义了Windows版本:

#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. 编辑:根据mingw.org文档,我了解到必须在编译中链接一个lib文件,大概是在源文件名之后加上-l标志。 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: 快速查找/ nm(具有识别Windows二进制文件的适当nm)0可以得出:

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. 看起来-luuid就是您所需要的。

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:~$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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