GCC warning when using CAPI calling convention - haskell

When using CAPI like this:
// libvlc.h
libvlc_instance_t *libvlc_new (int argc, const char *const *argv)
// VLC.hs
foreign import capi "vlc/libvlc.h libvlc_new" vlcNew :: CInt -> Ptr CString -> IO (Ptr Libvlc)
I'm getting the warning
/tmp/ghc3011_0/ghc3011_0.c: In function ‘ghc_wrapper_d18b_libvlc_new’:
/tmp/ghc3011_0/ghc3011_0.c:10:1:
warning: passing argument 2 of ‘libvlc_new’ from incompatible pointer type [enabled by default]
In file included from /tmp/ghc3011_0/ghc3011_0.c:7:0:
/usr/include/vlc/libvlc.h:138:1:
note: expected ‘const char * const*’ but argument is of type ‘HsInt8 **’
Compiles cleanly when using ccall.
It seems to be fine, but it's still a warning. Is there anything I can do about it?

Related

GCC interprets uint8_t and uint16_t as signed? [duplicate]

This question already has answers here:
Format specifiers for uint8_t, uint16_t, ...?
(7 answers)
Closed 3 years ago.
My test code:
#include <cstdint>
#include <cstdio>
int main() {
const constexpr uint8_t x = 64;
printf("%u", x);
}
Here is how I compiled with GCC 8.2:
g++ -Wall test_format.cpp -o test_format -O3 -std=c++17 -Wformat-signedness
And here is GCC's output:
test_format.cpp: In function ‘int main()’:
test_format.cpp:6:9: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int’ [-Wformat=]
printf("%u", x);
^~~~
Tho, if I try to print an uint32_t, it has no error/warning.
I wonder why GCC expects uint8_t to be signed int.
Thanks.
Default argument promotions are applied to operands of a variadic function. Under these, an expression of type unsigned char is promoted to int.
In C and C++ types narrower than int are always promoted to int. See Why must a short be converted to an int before arithmetic operations in C and C++?
And inside variadic functions default promotion also applies, which means you can't pass types narrower than int to vararg functions. So uint8_t must be printed with %d, not %u. But anyway you're printing it the wrong way. The correct way is to use PRIu8
printf("%" PRIu8 "\n", x);
Format specifiers for uint8_t, uint16_t, ...?
printing the uint8_t
Why is the format specifier for uint8_t and uint16_t the same (%u)?
How do I print uint32_t and uint16_t variables value?
To print a uint8_t variable with printf(), you should do something like the following:
#include <cinttypes>
#include <cstdio>
int print_u8(std::uint8_t x) {
return std::printf("%" PRIu8 "\n", x);
}
The <cinttypes> header includes printf and scanf format specifiers for all the <cstdint> types (and explicitly includes that header) that should be used for maximum portability.

error: 'SetPosition' was not declared in this scope

Arduino: 1.6.9 (Windows 10), Board: "Arduino Mega ADK"
In file included from C:\Users\Disheet\Downloads\humanoid_1\humanoid_1.ino:1:0:
C:\Users\Disheet\Documents\Arduino\libraries\ax12v2/ax12.h:66:23: error: conflicting declaration 'typedef unsigned char boolean'
typedef unsigned char boolean;
^
In file included from sketch\humanoid_1.ino.cpp:1:0:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:117:14: error: 'boolean' has a previous declaration as 'typedef bool boolean'
typedef bool boolean;
^
C:\Users\Disheet\Downloads\humanoid_1\humanoid_1.ino: In function 'void setup()':
humanoid_1:5: error: 'SetPosition' was not declared in this scope
SetPosition(1,0);////id,posiotin 0-1023
^
C:\Users\Disheet\Downloads\humanoid_1\humanoid_1.ino: In function 'void loop()':
humanoid_1:13: error: 'SetPosition' was not declared in this scope
SetPosition(1,512);
^
Multiple libraries were found for "ax12.h"
Used: C:\Users\Disheet\Documents\Arduino\libraries\ax12v2
Not used: C:\Users\Disheet\Documents\Arduino\libraries\Bioloid
exit status 1
'SetPosition' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
You'll need to find typedef unsigned char boolean; in your library and change it to match the version in Arduino.h.
boolean is already a typedef in Arduino.h, and it is a bool ,not unsigned char.
In the AX12 library search for this:
https://github.com/7Robot/Arduino/blob/master/AX12/libraries/ax12/ax12.h#L66
And change it to typedef bool boolean;.
This was updated a while ago, so your IDE version is newer than the AX12 library.

creating LPCTSTR conversion operator

Trying to create LPCTSTR conversion operator for my class:
AuthData::operator LPCTSTR() const
{
const char* k = "aaaa";
return k;
}
Error: return value type does not match the function type
Why it don't match?
If Unicode Character Set is enabled, LPCTSTR type is const wchar_t *.
#Edit
You can read about these types here:
https://softwareengineering.stackexchange.com/a/194768
In a Multi-Byte project, LPCTSTR is defined as LPCSTR, which is CONST CHAR*, so your code should compile (if it is in fact non-Unicode).
NEVER mix TCHARs with chars! At least - not without a conversion. Your declaration practically implies that TCHAR is defined as char, so why would you suggest to the readers that you are TCHAR-aware (since you are not)?

sprintf (convert int to char[])

In my program I try to convert a int to a char[20];
I try to do this in the following way:
char str[20];
sprintf(str, "%d", timer);
in which timer is the int.
But when I build this code, I get the following warnings.
Type implicit declaration of function 'sprintf' [-Wimplicit-function-declaration]
incompatible implicit declaration of built-in function 'sprintf' [enabled by default]
what does that mean?
note:( I have included string.h and stdlib.h).
great, I added stdio.h to my code and now the warnings disappeared only to give me a even harder error.
undefined reference to `_sbrk'
You have to #include <stdio.h> to use sprintf()
you want to make sure you also add reference to stdio.h see this ref
You probably need to put sprintf(str, "%d", timer) inside a function (not on the global part of the source code).
Something like:
#include <stdlib.h>
char str[20];
// SPOT #1
int f() {
sprintf(str, "%d", timer); // this won't work if placed on SPOT #1
}

C2664 error stream.write unsigned char *

ostream &stream;
stream.write(SomeUnsignedCharStar, intSize);
error C2664 cannot convert parameter 1 from const unsigned char * to const char *
Is there an overload write for const unsigned char *?
I do not want to change SomeUnsignedCharStar because it is everywhere in the legacy code I inherited. This was compiled on VC6 with no complain. I am slowly upgrading the code to VS2003 and then VS2010 evantually.
What is the easiest and cleanest fix?
You can cast this without any issues. Strict aliasing allows casting pointers between unsigned and signed versions of the same type, as well as casting from any type to const char*, so you're safe here.

Resources