C programming with FreeBSD

What are the first basic steps to do this with FreeBSD?
I open an editor such as VI and write the code? Then how do I invoke the compiler? I'm imagining I need a folder setup to save to in advance?

And this would be a similar process for other programming as well?
 
Create a directory, if you want. You can also store the source code and executable in your home directory. Or on a floppy or a deck of cards, but that would not be hip and fashionable.

Use an editor of your choice to write the code, for example foo.c. In practice, the code will end up being more than one file, and you will probably want a separate directory for it all. By the way, the question which editor to use is a religion, and many people will crucify you for suggesting vi; others will draw and quarter you if you use anything other than vi or one of its decedents. Don't blindly listen to people in either religious extreme, but consider their advice thoughtfully.

For starters, compile it directly with: "cc foo.c -o foo". You probably want to add switches like -Wall. To execute your program, you can just do "./foo".

The moment your source code is more than one file, you probably want to have a makefile, because trying to remember all the cc incantations required to compile different files will make your brain rot. At that point, compiling becomes much easier: Just say "make". On the other hand, writing makefiles from scratch ... will make hair grow on your chest, or fall out on your head.

What do you want to do with the program? If you want to always have it available, you probably want to create a ~/bin directory, put the final result in there, and add that to your path (by adding your shell login or startup file). If you are the system administrator or operated, you might want to place it in /usr/local/bin, and you should learn about the "install" program and root permissions.

If you change your program, you probably want to have some source code control mechanism, so you can describe the changes you made in a log file, go back to older versions, and share it with other computers and other people. That used to be rcs, then cvs. Today many people use git, but I happen to dislike it and use Mercurial (a.k.a. hg). Again, there are strong emotions and religions associated with tool to use. By the way, the term "git" means "an unpleasant or contemptible person", and the user interface of git is so awful, it makes the three people after whom it might be named (Linus, Larry and Tridge) seem like saints (Tridge is a really nice guy by the way, the other two ... not so much). If you don't know git yet, I suggest learning some other tool first.
 
What are the first basic steps to do this with FreeBSD?
As in any other unix[like] system:
1. write your source code with the texteditor of your choice (preinstalled on FreeBSD are vi, and ee; install other(s) you like.)
2. (in shell) compile the source e.g. cc my_hello_world.c
3. correct it, if there were bugs 😁
4. run your program e.g. ./a.out

The standard default command on any unix[like] system is always cc for "c compiler",
that's a link to the system's primary default c-compiler which in FreeBSD is clang(1),
https://en.wikipedia.org/wiki/Clang , https://clang.llvm.org/
You may also use options after cc as you were use the compiler with its direct name:
cc my_hello_world.c -o hello
./hello

I'm imagining I need a folder setup to save to in advance?
You don't need any. You can save your C source files anywhere on the system where you have write permissions, and compile it either in the according directory, or tell cc the path to the file.
But it would make pretty much sense to stay in your own /home/ directory, and, yes, to create there directories not to mess up the place, but keep files in order not to lose surveillance.

If you're going to do some programming more than just a bit more as "hello world!", you are adviced best to think of a sophisticated directory tree (good names!), start to use some version control (most people use git, except me; but I wouldn't recommend any but to use one anyway), and, yes, as ralphbsz already said, learn to use make
But then we're already beyond what I would call "first steps" :cool:

And this would be a similar process for other programming as well?
Yes. (Well, not for shell scripts. You don't need to compile those, but simply run them.)
And you were best adviced to have own directories for each language. But of course that's completely up to you, how you organize your workplace, what's comfortable to your work style, and how you not lose surveillance over your stuff.

It's impossible to answer that question in general.
There are too many choices, possibilities, personal flavors, and work styles as there can be a satisfying answer given to that, but you need to find out for youself: what there is, what you need, what suits you. And this can be said for the whole system, not just programming.

There is lots of stuff you can add. Like nxjoseph already pointed out, there is not only the choice of the editor, and several plug-ins for each (emacs can be really bloated), and other things like IDEs (I believe [I'm not a big fan of "jack-of-all-trades"-things, but prefer good old unix-style: small tools, specialized for each job, almost always in the shell. But others tell you otherwise. You have to figure out yourself what suits you best, or even develop your own work style, or change it to unix-like.])

My advice was:
Start small and simple (as I described above. You already can do a lot with "basic vanilla", by principle already everything. Anything you add is to help organize your stuff.)
Add what you need, and only what you really need.
Otherwise there is a real chance you may drown in the vast ocean of options, only configuring and learning your new tools, but not doing any programming anymore. 🤓
 
1. Install clang
2. Try nano / pico

I prefer to use Visual Studio from Windows as editor and then compile in console (via PuTTY).
 
All overly complicated. All you need is a sharp pencil. And those punched cards where individual holes can be punched out by pushing with the point of the pencil. And then you memorize the EBCDIC punched card code (it's easy, single hole for digits, two holes for letters).

Oh wait, you were asking for the BEST possible editor? I just told you about the worst possible editor.
 
1. Install clang
2. Try nano / pico

I prefer to use Visual Studio from Windows as editor and then compile in console (via PuTTY).
I'm having a debate with myself on that. I haven't used windows in 20 years, but every once in awhile I'd like to know what some people mean when they refer to it. But I don't need windows for pico or nano do I? I just ordered a used Laptop and it came with win 11 and I can't decide if I want to keep the spyware and use it in virtual mode and use the FreeBSD that way or wipe the drive and use FreeBSD to run everything else in virtual mode. Or.... use what I'm used to using and run FreeBSD from there. The 2TB drive arrives next week
 
All overly complicated. All you need is a sharp pencil. And those punched cards where individual holes can be punched out by pushing with the point of the pencil. And then you memorize the EBCDIC punched card code (it's easy, single hole for digits, two holes for letters).

Oh wait, you were asking for the BEST possible editor? I just told you about the worst possible editor.
Those weren't so bad. Try fixing a papertape editor with worn rollers and dull cutters, whatever the they were called. It was an ordeal.
 
And women were women, and little furry creatures from Alpha Centauri were little furry creatures from Alpha Centauri.
Is that where the Furlings live? O'neall got stranded on their moon? Yes, that is why I didn't get my degree in programming yet, SG1 got in the way.
 
I've noticed no one has suggested Geany. I stumbled onto it when I got Nomad to work on my laptop, there was a teapot in the menu.
 
  • Like
Reactions: mro
Back
Top