Wang Liang
Wang Liang

Reputation: 4434

MAC: Qt Cannot detect toolkit

Screen

How I can configure QT Project using Qt 5.12.1/Qt Creator 4.8.1
I need compile my project for mac os.
I tryed with QT Creator, then, below problem occur.

Or compile with below command.

qmake
make

But, below error is occur.

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 Of course, I can change some code with typecast, ex.) char* to void*.
But, there are many macros, and codes, so, many code change is need for "remove type cast".
Of course, I used "-fpermissive" flags, and my project is compiled at Windows 10.

So, How I can compile my project without code change?

EDIT:

Here is a MRE of my problem:

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

This code is compiled at 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)

But, I cannot compile at My Mac OS.

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$ 

Upvotes: 2

Views: 390

Answers (2)

Alan Birtles
Alan Birtles

Reputation: 36389

The simple answer is "don't use malloc in c++", if you use new instead the problem goes away and as a bonus you correctly initialise classes.

If you really must use malloc you can't avoid the need to static_cast the result into the appropriate type.

If you really don't want to add static casts everywhere then something like this might work:

struct mymalloc
{
    mymalloc(size_t size)
    : data(malloc(size))
    {
    }
    mymalloc(const mymalloc&) = delete;
    mymalloc& operator=(const mymalloc&) = delete;

    void* data;

    template <typename T>
    operator T*()
    {
        return static_cast<T*>(data);
    }
};

int main()
{
    char *buffer = mymalloc(10);
    free(buffer);

    return 0;
}

You might even have some success with #define malloc mymalloc but you'd need to be very careful not to break anything else by doing that.

I repeat though that the proper solution is to switch from malloc to new and avoid all this hackery.

Upvotes: 2

dbush
dbush

Reputation: 223872

This is due to a difference in how C and C++ handle conversions to a from a void *. In C, any pointer type may freely be converted to/from a void * without a cast. C++ does not allow this, and will throw an error if you attempt to convert without a cast.

The code in your example is C code but the filename ends in .cpp, so gcc compiles the code as C++. So this line:

buffer = malloc(10);

generates an error because C++ doesn't allow an implicit void * conversion.

If you changed the filename from test.cpp to test.c it will compile cleanly.

Going back to your QT project, since QT is a C++ library you can't just attempt to compile all of the code as C code.

If the modules that have these errors contain only C code then you can change the filename of those modules from .cpp to .c, and you'll need to modify the associated header files to add extern "C" around the declarations of the functions. If these errors appear in modules that actually are C++, then you need to change your code to use static_cast<char*> to assign the return value of malloc/calloc/realloc to a char *.

Upvotes: 3

Related Questions