C Need guidance for programming in C on FreeBSD

System: FreeBSD 14

I'm new to FreeBSD, coming from Linux and have always used gcc.
In FreeBSD so I can compile in C and C++ what should I use?

Thanks.
 
I'm new to FreeBSD, coming from Linux and have always used gcc.
In FreeBSD so I can compile in C and C++ what should I use?

The canonic name for the system's C compiler has always been cc on any "unixy" system. It should always be present when the system has a C compiler. If cc does not work on your Linux system, something is broken there.

FreeBSD uses LLVM's clang and not GNU's gcc to provide cc. In many cases, cc will actually be a symlink or hardlink.[*]

When you write some Makefile and want to be portable, always default to using cc, and additionally provide a way for the user to override it by setting the CC make variable.

---
[*] On FreeBSD, it's a hardlink, see
Code:
$ ls -li /usr/bin/clang
496856 -r-xr-xr-x  6 root  wheel  94263896 19 März 11:38 /usr/bin/clang
$ ls -li /usr/bin/cc
496856 -r-xr-xr-x  6 root  wheel  94263896 19 März 11:38 /usr/bin/cc
(same inode number)

edit: Indeed, what SirDice said, many compilers are available from ports. But you should always use the system's default/base compiler, unless there's some very specific need for a specific compiler and version, which is then typically outside the scope of the C language standard. Explicitly typing gcc on a Linux system is a bad habit (there should be a cc as well invoking the very same compiler), and turns into a portability annoyance when used in Makefiles and similar for systems that don't use gcc ....

----

Yet another edit, I overlooked you also asked about C++. Well, basically the same. The canonic name is c++ and should always work when a system default C++ compiler is installed. GCC's compiler is named g++, LLVM's compiler clang++, but don't use these names unless you mean specifically that compiler.

And SirDice, cpp is the canonic name for the C preprocessor (rarely invoked directly), so don't confuse that 😏
 
For a start I would advise against learning C. It has almost no type or memory safety. There‘s just about nothing that you can do in C that can’t be done in C++.

You can fairly freely mix code and libraries from C compilers (one of its few advantages - a bit like saying that a person with a mental age of 5 has the advantage that everyone can understand the words that they use). That isn’t true for C++ compilers. Unless you really need something not available in clang++ or libc++ then I recommend sticking to them.
 
As if C++ was any better.
With C you can do anything. It interfaces with everything. It's used everywhere.
Type and memory safety is the purview of the programmer. It's their job. Lack of safety is failure of the programmer. Quit blaming the tool.
 
For a start I would advise against learning C. It has almost no type or memory safety.
In my opinion, this isn't good advice. Neither C nor C++ have "memory safety", this just isn't possible with explicit memory management, C++ just offers lots of (more or less complex) helper constructs for common things. C has strong compile-time type safety (you just have to be aware of some implicit conversions regarding numeric types, but that's the same in C++), but also a way around it: void *, and unfortunately you will need that as soon as you need anything "generic". C++ "solves" this with templates which provide some form of generics, but aren't really generics (that's simplified and going into detail here is out of scope).

Regarding the advice, mine would be the opposite: Learn C first (or maybe skip C and C++ altogether). Learning C properly will teach you the important basics. Debugging C code is a lot easier (what you see in a debugger with C++ easily overwhelms a beginner). Some of the more obscure issues you can run into with C++ are easier to understand when you already have experience with a simpler language (e.g. it helps a lot to fully understand object lifetime and scope as well as declaration vs definition in C before tackling C++).

I personally don't like C++ (had design flaws from the very beginning resulting from the conflicting goals: "advanced" OOP language features plus full C compatibility at that time, and then every new version adds tons of complex stuff ...), but still use it when there's a good reason for me. Otherwise I prefer to stick to C.

BTW, objective advantages of C are: It compiles a LOT faster than C++ code and there are compilers available for more (including exotic) target platforms.
 
The OP's question seems a bit vague to me - sounds like they may already be familiar with C/C++, but from using GCC under Linux.

Not sure if they are asking what language they should learn to do "stuff" in FreeBSD itself, or just asking what compiler they can use instead of GCC?

Or maybe they mean to actually write code that will just run on FreeBSD - in which case there are a lot of options.
 
For a start I would advise against learning C. It has almost no type or memory safety. There‘s just about nothing that you can do in C that can’t be done in C++.

You can fairly freely mix code and libraries from C compilers (one of its few advantages - a bit like saying that a person with a mental age of 5 has the advantage that everyone can understand the words that they use). That isn’t true for C++ compilers. Unless you really need something not available in clang++ or libc++ then I recommend sticking to them.

Thank you for expressing your opinion and advice, all information is welcome, because often the other party may have information that we do not have.
I'll explain what I've been doing...

I learned programming, in my opinion the wrong way! I started with Delphi and thought I was a programmer, until I discovered that Delphi entered thousands of codes that I didn't even know existed. I felt like a failure, because I realized that I didn't know anything about programming.

When I started with C along with C++, I needed C++ because of the graphics libraries, but obviously I didn't really enjoy C++ writing code instead. An example is "string". No string exists.
I like C because I learn exactly how things work, and better than C is Assembly, which I really want to learn.

By necessity I set up a small database to record my learning, in a hurry I used a lot of C++ and I am currently rewriting its code to C, using C++ only in the graphics libraries. Is it work? Yes, but it is visible code and controlled by me.

Maybe in the future, when I really master how programming works, I can afford to use a higher-level tool to speed up the creation of a program, but at the moment I don't need that rush.
 
The OP's question seems a bit vague to me - sounds like they may already be familiar with C/C++, but from using GCC under Linux.

Not sure if they are asking what language they should learn to do "stuff" in FreeBSD itself, or just asking what compiler they can use instead of GCC?

Or maybe they mean to actually write code that will just run on FreeBSD - in which case there are a lot of options.

My question refers to the compiler. When doing a small test with clang I was able to notice that gcc wrote codes in my place, and I didn't like that at all, where I give points to clang that refused to compile my code due to the lack of included libraries.

When I write a program I like to be 100% sure that everything for it to work is there. And when gcc decided to write it for me without telling me anything, this is disastrous, and because of that, I will have to re-write several examples that I saved, because headers and libraries are missing because of this.

Because of this, I really liked clang at first glance.

The answers above, where they say that FreeBSD uses clang, helped me a lot. And before I thought gcc was wonderful, gcc disappointed me!
 
Huh, I must have missed something. How does C do RAII? And templates?
Not natively but you can make your own. Then you can call it by a name that makes sense to you. And it will be smaller and faster with more features (or less if you wish).
How do you do an interface with postgresql? They only have a C interface. Yeah, I know. You can probably emulate the C interface with C++. Or can you?
 
How do you do an interface with postgresql? They only have a C interface. Yeah, I know. You can probably emulate the C interface with C++. Or can you?
In c++, to access the C API described in foo.h:
Code:
extern "C" {
#include <foo.h>
}

And off you go. (Although many headers will already have that inside wrapped in a #ifdef __cplusplus block so you don’t end up needing it.)

Or were you asking something else?
 
Who needs Rust when there is Lisp?

lisp.jpg
 
I personally don't like C++ (had design flaws from the very beginning resulting from the conflicting goals: "advanced" OOP language features plus full C compatibility at that time, and then every new version adds tons of complex stuff ...), but still use it when there's a good reason for me. Otherwise I prefer to stick to C.

Don't go on about OO. It just shows that you are out of touch. Modern C++ is mostly about generic programming, with the OO still there.

Unfortunately C++ inherited a lot of C's failings: macros, type promotion, implicit type conversion. C++ has also made quite a few mistakes over the years.

Some progress has been made. For instance, brace initialization doesn't allow implicit type conversion.

Herb Sutter is doing some interesting stuff with cppfront https://github.com/hsutter/cppfront (there is also Carbon from Google but that's a bigger departure). I think that he's fairly well placed to criticise C++ (and propose fixes), being the convenor of WG21, the standardization body for C++. I have some hope that C++ will be able to bolt on an acceptable amount of memory safety (for instance, with Bjarne Stroustrup's proposals for Profiles).

And for the mention of Rust. My younger son is interested in studying computer science. I've recommended that he avoid C++ and consider learning Rust.
 
Without knowing that clang was already installed by default, I installed the version: llvm17: 17.0.6_3
Now when I check to understand, things get messy!
It was understandable to have 2 versions, but to have 3?

# /usr/local/bin/clang15 --version clang version 15.0.7 Target: x86_64-portbld-freebsd14.0 Thread model: posix InstalledDir: /usr/local/llvm15/bin
# /usr/local/bin/clang17 --version clang version 17.0.6 Target: x86_64-portbld-freebsd14.0 Thread model: posix InstalledDir: /usr/local/llvm17/bin
# cc --version FreeBSD clang version 16.0.6 (https://github.com/llvm/llvm-project.git llvmorg-16.0.6-0-g7cbf1a259152) Target: x86_64-unknown-freebsd14.0 Thread model: posix InstalledDir: /usr/bin
 
One probably came in with Xorg.
I think the amdgpu stuff requires a specific version of LLVM. Bit messy (especially if you have an i.e Intel GPU).
 
Back
Top