c++ - MAC : Qt Cannot detect toolkit

标签 c++ macos qt gcc

Screen

如何使用 Qt 5.12.1/Qt Creator 4.8.1 配置 QT 项目
我需要为 mac os 编译我的项目。
我尝试使用QT Creator,然后,出现以下问题。

或者使用以下命令进行编译。

qmake
make

但是,出现以下错误。

third_party/wfdbio.cpp:181:5: fatal error: assigning to 'char *' from
      incompatible type 'void *'
    SSTRCPY(wfdbpath, wfdbpath_init);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
third_party/wfdb.h:234:3: note: expanded from macro 'SSTRCPY'
         SALLOC(P, (size_t)strlen(WFDB_tmp)+1,1); strcpy(P, WFDB_tmp); } }
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
third_party/wfdb.h:231:37: note: expanded from macro 'SALLOC'
#define SALLOC(P, N, S) { SFREE(P); SUALLOC(P, (N), (S)) }
                                    ^~~~~~~~~~~~~~~~~~~~
third_party/wfdb.h:230:38: note: expanded from macro 'SUALLOC'
#define SUALLOC(P, N, S) { if (!(P = calloc((N), (S)))) MEMERR(P, (N), (S)); }
                                     ^~~~~~~~~~~~~~~~
1 error generated.
make: *** [objects/wfdbio.o] Error 1

Error Screen2 当然,我可以使用类型转换来更改一些代码,例如)char* to void*
但是,有很多宏和代码,因此,需要更改许多代码来“删除类型转换”。
当然,我使用了“-fpermissive”标志,并且我的项目是在 Windows 10 上编译的。

那么,如何在不更改代码的情况下编译我的项目?

编辑:

这是我的问题的 MRE:

#include <stdio.h>
#include <stdlib.h> 
int main()
{
    char *buffer;

    printf("How long do you want the string? ");

    buffer = malloc(10);
    // buffer = (char* ) malloc(10);

    return 0;
}

此代码是在Windows10 Cygwin上编译的。

# At Cygwin, gcc version is
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libcilkrts --enable-libgomp --enable-libitm --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
gcc version 7.4.0 (GCC)

但是,我无法在我的 Mac 操作系统上进行编译。

Below is error code.

Ws-Mac:Desktop w$ gcc test.cpp -fpermissive
test.cpp:9:14: error: assigning to 'char *' from incompatible type 'void *'
    buffer = malloc(10);
             ^~~~~~~~~~
1 error generated.
# At Mac OS, gcc version is
Ws-Mac:Desktop w$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Ws-Mac:Desktop w$ 

最佳答案

这是由于 C 和 C++ 处理从 void * 到 a 的转换的方式不同所致。 。在 C 中,任何指针类型都可以自由地与 void * 相互转换。没有 Actor 。 C++ 不允许这样做,如果您尝试在不进行强制转换的情况下进行转换,将会抛出错误。

示例中的代码是 C 代码,但文件名以 .cpp 结尾,因此 gcc 将代码编译为 C++。所以这一行:

buffer = malloc(10);

生成错误,因为 C++ 不允许隐式 void *转换。

如果将文件名从 test.cpp 更改为 test.c,它将可以干净地编译。

回到您的 QT 项目,由于 QT 是一个 C++ 库,您不能尝试将所有代码编译为 C 代码。

如果出现这些错误的模块包含 C 代码,那么您可以将这些模块的文件名从 .cpp 更改为 .c,并且您需要修改关联的头文件以添加extern "C"围绕函数的声明。如果这些错误出现在实际上是 C++ 的模块中,那么您需要更改代码以使用 static_cast<char*>分配返回值 malloc/calloc/reallocchar * .

关于c++ - MAC : Qt Cannot detect toolkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59880002/

相关文章:

c++ - 将指针传递给需要数组的函数是否合法?

c++ - Cout 和 endl 错误

c++ - 错误 c2953 : class template has already been defined

macos - 体系结构 x86_64 : "_glBegin" 的 undefined symbol

c++ - 根据三元表达式声明变量类型

python - 如何为包含动画元素的项目设置 QtDesigner?

c++ - 引用动态分配的对象 ("at run-time")

java - OS X 上的 JVM 总是以状态 0 退出(如 Bash 脚本中捕获的那样)?

macos - 如何在 MACOS 终端中显示面包屑?

qt - 项目错误: Unknown module(s) in QT: multimedia