What is the FreeBasic equivalent to "char" in C++?

General FreeBASIC programming questions.
Post Reply
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

What is the FreeBasic equivalent to "char" in C++?

Post by angros47 »

Declaring a procedure using extern "c++", the type "byte" is converted to "signed char", while the type "ubyte" is converted to "unsigned char". What if the C++ function used just "char", without specifying?
In a C declaration this is not a problem, but in a C++ declaration the name mangling is different if the function uses "char" or "signed char"
Laurens
Posts: 17
Joined: Mar 16, 2022 9:16
Location: Flevoland, the Netherlands

Re: What is the FreeBasic equivalent to "char" in C++?

Post by Laurens »

If you mean the format, you problably are looking for the string. For example, in c:

Code: Select all

int main() {

	// Initializing a variable:
	char character = 'x';

	// Printing the  variable:
	cout << "Character = " << character << endl;

    return 0;
}
The FreeBasic equivalent can look like:

Code: Select all


	' Initializing a variable:
	Dim As String character = "x"
	
	' Printing the  variable:
	Print character
    
	end 0
If you mean to convert to a character, the following would do:

Code: Select all

	' Initializing a variable:
	Dim As String Character
	Dim As Integer NumericValue = 89
	
	'Converting the integer value to string value:
	Character = Str (NumericValue)
		
	' Printing the  variable:
	Print Character
    
	End 0
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: What is the FreeBasic equivalent to "char" in C++?

Post by angros47 »

No. I am talking about C++, not C

If in C++ I have:

Code: Select all

void foo(char a)
And in FreeBasic I declare:

Code: Select all

extern "c++"
Declare sub foo (a as byte)
end extern
It will look for the C++ function

Code: Select all

void foo(signed char a)
And that will return an error (in C the two declarations are equivalent, in C++ they are not)
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: What is the FreeBasic equivalent to "char" in C++?

Post by caseih »

Internally the compiler has a type referred to as FB_DATATYPE_CHAR, which maps to C++ char, but I have no idea how to access that type from FB. It seems to be associated with ZString. In other words ZString consists of chars and is a char ptr, but there does not seem to be a way to use char directly from FB. If true, that's quite a curious omission/bug.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: What is the FreeBasic equivalent to "char" in C++?

Post by coderJeff »

https://github.com/freebasic/fbc/pull/386

Will allow:

Code: Select all

extern "c++"
declare sub proc1( byval a as byte alias "char" )   '' byte on fbc side; char on c++ side
declare sub proc2( byval a as ubyte alias "char" )  '' ubyte on fbc side; char on c++ side
end extern
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: What is the FreeBasic equivalent to "char" in C++?

Post by TeeEmCee »

angros47 wrote: Jul 26, 2022 7:30in C the two declarations are equivalent, in C++ they are not
They aren't equivalent in C either. Recent GCC versions will print a warning if you mix them. C has three distinct char types: char, signed char, unsigned char two of which can contain the same values. This is unlike all other integral types. It's a kludge in the C standard to allow C compiler to differ on whether char is signed or not, for backcompatibility with the earliest compilers. Ugh.

So the difference between C and C++ is that in C you get a warning which doesn't matter (although maybe it breaks code using C generics?) while in C++ it will break name mangling.

fbc translates zstring to ubyte, and that's arguably a bug due to the GCC warnings (or was that fixed/silenced already?). It's nice to see that PR for 'alias' to allow correct C++ mangling.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: What is the FreeBasic equivalent to "char" in C++?

Post by caseih »

Amazing to see how quickly this minor issue was addressed! Great job, @coderJeff!
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: What is the FreeBasic equivalent to "char" in C++?

Post by Axle »

Hi peoples

Can someone explain something as I have asked this question before.

Is FreeBASIC Compiler C or C++ based?
From all of what I read, and looking at the compiler FBC, it leaves me to believe that it is a C compiler, yet I repeatedly see people referring to using C++ in FB. Is this an addition somewhere in FBC to handle shared objects compiled in a C++ compiler? or is FBC some kind of hybred C slash C++ compiler under the hood?
Imortis
Moderator
Posts: 1923
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: What is the FreeBasic equivalent to "char" in C++?

Post by Imortis »

Axle wrote: Jul 27, 2022 10:12 Hi peoples

Can someone explain something as I have asked this question before.

Is FreeBASIC Compiler C or C++ based?
From all of what I read, and looking at the compiler FBC, it leaves me to believe that it is a C compiler, yet I repeatedly see people referring to using C++ in FB. Is this an addition somewhere in FBC to handle shared objects compiled in a C++ compiler? or is FBC some kind of hybred C slash C++ compiler under the hood?
FB is not based on either C or C++. It is based on QBASIC with some modern amenities. The compiler is written in FreeBASIC itself, with the runtime and graphics libraries written in C. It has backends that output ASM or C.

It is possible to interoperate with other languages, but the further way from FB's model it gets the more pain there is in the process. FB has a similar feature set to C, but is not 1-to-1, so some things work better/easier than others.

I hope this answers your question.
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: What is the FreeBasic equivalent to "char" in C++?

Post by angros47 »

@coderJeff
This is a great news!
Out of curiosity, does your adding allows also to fix the mangling in nested types? ( viewtopic.php?t=29524)
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: What is the FreeBasic equivalent to "char" in C++?

Post by caseih »

Axle wrote: Jul 27, 2022 10:12Is FreeBASIC Compiler C or C++ based?
FB uses the same "calling convention" as C does, so you can seamlessly call back and forth between FB and C, facilitated by the extern "C" construct in your FB code. FB uses the same kinds of variables as C also, and exports them to an object file in the same way, so variables in a C module can be accessed and changed from FB and vice versa. You can link FB and C code together in a single executable, or use C DLLs from FB (or vice versa).

C++ is a different animal entirely. FB has some support for calling C++ functions and instantiating some C++ objects using the extern "C++" construct. C++ allows multiple functions to have the same name but take different parameters. They are differentiated using "name mangling" where the parameter types are encoded into the name of the function. FB has some support for this name mangling, which is what this particular message topic is about. Because FB can interpret C++'s name mangling scheme, and because FB's object model has some things in common with C++, you can call functions in C++ from FB, and even instantiate and manipulate some simple C++ objects. However anything more advanced in C++ is inaccessible to FB. For example, multiple inheritance and template classes. Typically if need to interact with a C++ library from FB, your best bet is to use C++ to create a wrapper that exports pure C functions for FB code to work with, and manages all the C++ objects. This is true for nearly all other languages as well.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: What is the FreeBasic equivalent to "char" in C++?

Post by TeeEmCee »

Specifically, FB attempts (incomplete) ABI compatibility with GNU g++. There is no support for other C++ compilers (though I don't know how compatible g++ and clang++ are) . Likewise, its GCC emitter outputs C code that contains GCC-isms and can only be compiled with difficulty using clang (despite it being very highly GCC compatible), and not at all with non-GCC-compatible C compilers. Yet.
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: What is the FreeBasic equivalent to "char" in C++?

Post by Axle »

@Imortis @caseih
Thanks I appreciate you replies, but...
FB is not based on either C or C++. It is based on QBASIC with some modern amenities. The compiler is written in FreeBASIC itself
If you go to the compiler under \bin you will find gcc.exe which is a gcc compiler, not created in FB

C:\Dev-FreeBASIC\FreeBASIC-1.09.0-win64\bin\win64>gcc --version
gcc (MinGW-W64 x86_64-posix-sjlj, built by Brecht Sanders) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

So, this tells me that this is Gnu 'C' Compiler. Also FreeBASIC runtime are all created in GCC/Mingw aka GNU 'C' Compiler collection. So, I cannot see how FreeBASIC can compile C++ source code (Unless it is converted to C, or the C++ has been written to conform to C Standards instead of C++, which would just make in C source code anyway). Noting that FreeBASIC cannot actually compile most C source as the STD C libraries are not present in the FB compiler set.

So in every instance I see the FBC.exe precompiler can only compile C source to a binary via gcc.exe and is relent upon the separately C compiled archive files and shared objects. So I am guessing when anyone talks about using C++ they are actually just referring to dynamic linking (implicit/explicit) to a shared object compiled separately in C++, which we have some limited ability to do in C anyway.

So, am I correct in assuming that any discussion, or reference to C++ is really just a reference to runtime linking against a shared object .so/.dll compiled in C++? And that FBC.exe and gcc.exe can only compile C source and link C archives.a and C object.o?

Axle
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: What is the FreeBasic equivalent to "char" in C++?

Post by angros47 »

FreeBasic does not compile C++ code, FreeBasic compiles code that can interoperate with C++, it's not the same thing.

It is actually possible to call a C++ function from a C program: the calling convention is the same, the main difference is that in the object code the name of a function written in C is the same used in the source code, while the name of a function written in C++ is changed, adding the type of the arguments (this allows function overloading, because the overloaded functions will be considered as different ones). This is called "mangling". If, in C code, you add a declaration with a mangled name, you can call a C++ function.

FreeBasic does support function overloading, and this means it has its own name mangling: it is also able, when 'extern "c++" ' is used, to use the same rules of name mangling of C++, and this means that the C code (or the ASM code) created by FreeBasic can call C++ code, even without a C++ compiler
Post Reply