Linux C PreProcessor: Who Knew?
-
I'm going through a book right now which is fantastic! It's the best intro to OSes (how they are designed/programmed) I believe you'll ever read. First of all, you can read it for FREE online (PDFs). Operating Systems: Three Easy Pieces[^] Have you read the book? It's fantastic because it starts out with simple code examples (in C) that teach one specific point at a time. Amazing! If you've never read it, please skip all the (intro nonsense) & go right for the meat (to get an idea of how great the book is). Read this chapter[^] (be aware this will open the PDF in your browser) & I believe you'll be convinced how great the book is too. And Now For the Weird & Wonderful While examining the common_threads.h file for the 3rd example I stumbled upon these lines:
#ifdef __linux__
#include #endifI always wonder about strange things like that and in this case I thought, "Where is the __linux__ defined?" I looked it up and discovered Gnu docs for the C PreCompiler (cpp exe)[^]. I tried the command they suggested:
$ cpp -dM
But when I did I could see that cpp was running but just had an empty string. You actually have to point it at an exe and it'll pull out all the preprocessor commands. What!?!
$ cpp -dM ./threadx
When I ran that I saw a huge list of PreProcess commands. Huge! I then ran it thru grep like this:
$ cpp -dM ./iox | grep -i linux
I saw the following!!
#define __linux 1
#define __gnu_linux__ 1
#define linux 1
#define __linux__ 1So, I can see that over the years the convention to determine which OS the compiler is running on has changed. :wtf: Wow, things get messy as time goes on, don't they. BONUS Here's my favorite pre-processor define I found in the list. :rolleyes:
#define __FLT128_DENORM_MIN__ 6.47517511943802511092443895822764655e-4966F128
-
I'm going through a book right now which is fantastic! It's the best intro to OSes (how they are designed/programmed) I believe you'll ever read. First of all, you can read it for FREE online (PDFs). Operating Systems: Three Easy Pieces[^] Have you read the book? It's fantastic because it starts out with simple code examples (in C) that teach one specific point at a time. Amazing! If you've never read it, please skip all the (intro nonsense) & go right for the meat (to get an idea of how great the book is). Read this chapter[^] (be aware this will open the PDF in your browser) & I believe you'll be convinced how great the book is too. And Now For the Weird & Wonderful While examining the common_threads.h file for the 3rd example I stumbled upon these lines:
#ifdef __linux__
#include #endifI always wonder about strange things like that and in this case I thought, "Where is the __linux__ defined?" I looked it up and discovered Gnu docs for the C PreCompiler (cpp exe)[^]. I tried the command they suggested:
$ cpp -dM
But when I did I could see that cpp was running but just had an empty string. You actually have to point it at an exe and it'll pull out all the preprocessor commands. What!?!
$ cpp -dM ./threadx
When I ran that I saw a huge list of PreProcess commands. Huge! I then ran it thru grep like this:
$ cpp -dM ./iox | grep -i linux
I saw the following!!
#define __linux 1
#define __gnu_linux__ 1
#define linux 1
#define __linux__ 1So, I can see that over the years the convention to determine which OS the compiler is running on has changed. :wtf: Wow, things get messy as time goes on, don't they. BONUS Here's my favorite pre-processor define I found in the list. :rolleyes:
#define __FLT128_DENORM_MIN__ 6.47517511943802511092443895822764655e-4966F128
raddevus wrote:
First of all, you can read it for FREE online (PDFs). Operating Systems: Three Easy Pieces[^]
When I open the link, it tells me that the PDF version is USD 10. I am not that curious about the book.
Religious freedom is the freedom to say that two plus two make five.
-
I'm going through a book right now which is fantastic! It's the best intro to OSes (how they are designed/programmed) I believe you'll ever read. First of all, you can read it for FREE online (PDFs). Operating Systems: Three Easy Pieces[^] Have you read the book? It's fantastic because it starts out with simple code examples (in C) that teach one specific point at a time. Amazing! If you've never read it, please skip all the (intro nonsense) & go right for the meat (to get an idea of how great the book is). Read this chapter[^] (be aware this will open the PDF in your browser) & I believe you'll be convinced how great the book is too. And Now For the Weird & Wonderful While examining the common_threads.h file for the 3rd example I stumbled upon these lines:
#ifdef __linux__
#include #endifI always wonder about strange things like that and in this case I thought, "Where is the __linux__ defined?" I looked it up and discovered Gnu docs for the C PreCompiler (cpp exe)[^]. I tried the command they suggested:
$ cpp -dM
But when I did I could see that cpp was running but just had an empty string. You actually have to point it at an exe and it'll pull out all the preprocessor commands. What!?!
$ cpp -dM ./threadx
When I ran that I saw a huge list of PreProcess commands. Huge! I then ran it thru grep like this:
$ cpp -dM ./iox | grep -i linux
I saw the following!!
#define __linux 1
#define __gnu_linux__ 1
#define linux 1
#define __linux__ 1So, I can see that over the years the convention to determine which OS the compiler is running on has changed. :wtf: Wow, things get messy as time goes on, don't they. BONUS Here's my favorite pre-processor define I found in the list. :rolleyes:
#define __FLT128_DENORM_MIN__ 6.47517511943802511092443895822764655e-4966F128
I've got the dead tree version on my shelf. It is a very good book.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak
-
raddevus wrote:
First of all, you can read it for FREE online (PDFs). Operating Systems: Three Easy Pieces[^]
When I open the link, it tells me that the PDF version is USD 10. I am not that curious about the book.
Religious freedom is the freedom to say that two plus two make five.
It opened for me, no messages. What browser are you using??
__________________ Lord, grant me the serenity to accept that there are some things I just can’t keep up with, the determination to keep up with the things I must keep up with, and the wisdom to find a good RSS feed from someone who keeps up with what I’d like to, but just don’t have the damn bandwidth to handle right now. © 2009, Rex Hammock
-
raddevus wrote:
First of all, you can read it for FREE online (PDFs). Operating Systems: Three Easy Pieces[^]
When I open the link, it tells me that the PDF version is USD 10. I am not that curious about the book.
Religious freedom is the freedom to say that two plus two make five.
I re-tried both links (the one to the book's main web site) and the one directly to the PDF & both worked for me. Honestly the book is free so I hope you get a chance to take a look. Someone else has confirmed that they links worked too. Try the link to the main site and then try clicking the chapters in the grid. Maybe you'll be able to get it there.
-
I've got the dead tree version on my shelf. It is a very good book.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak
Dave Kreskowiak wrote:
I've got the dead tree version on my shelf.
Very cool that you've read it. I am really enjoying the code samples, because they : 1. touch on very basic but very clear topics 2. are self contained & don't require a lot to get them compiled. 3. Touch on important topics like file i/o, simple threading ideas, etc. Really amazing book so far.
-
I'm going through a book right now which is fantastic! It's the best intro to OSes (how they are designed/programmed) I believe you'll ever read. First of all, you can read it for FREE online (PDFs). Operating Systems: Three Easy Pieces[^] Have you read the book? It's fantastic because it starts out with simple code examples (in C) that teach one specific point at a time. Amazing! If you've never read it, please skip all the (intro nonsense) & go right for the meat (to get an idea of how great the book is). Read this chapter[^] (be aware this will open the PDF in your browser) & I believe you'll be convinced how great the book is too. And Now For the Weird & Wonderful While examining the common_threads.h file for the 3rd example I stumbled upon these lines:
#ifdef __linux__
#include #endifI always wonder about strange things like that and in this case I thought, "Where is the __linux__ defined?" I looked it up and discovered Gnu docs for the C PreCompiler (cpp exe)[^]. I tried the command they suggested:
$ cpp -dM
But when I did I could see that cpp was running but just had an empty string. You actually have to point it at an exe and it'll pull out all the preprocessor commands. What!?!
$ cpp -dM ./threadx
When I ran that I saw a huge list of PreProcess commands. Huge! I then ran it thru grep like this:
$ cpp -dM ./iox | grep -i linux
I saw the following!!
#define __linux 1
#define __gnu_linux__ 1
#define linux 1
#define __linux__ 1So, I can see that over the years the convention to determine which OS the compiler is running on has changed. :wtf: Wow, things get messy as time goes on, don't they. BONUS Here's my favorite pre-processor define I found in the list. :rolleyes:
#define __FLT128_DENORM_MIN__ 6.47517511943802511092443895822764655e-4966F128
You can get a list of the compiler Manifest Defines via
echo | cc -dM -E -
In my case, I get 442
#defines
for c++, and 380 for cc. Interestingly, the C compiler and the pre-processor produce exactly the same manifest defines. Messing around with different C/C++ standards gets different values for different standard versions e.g-std=c89
or-std=gnu17
or-std=gnu++11
, so might be worth inspecting, to see how you can tell whether you're compiling for C89, C99, or if you're using clang (#define __clang__
), etc"A little song, a little dance, a little seltzer down your pants" Chuckles the clown
-
You can get a list of the compiler Manifest Defines via
echo | cc -dM -E -
In my case, I get 442
#defines
for c++, and 380 for cc. Interestingly, the C compiler and the pre-processor produce exactly the same manifest defines. Messing around with different C/C++ standards gets different values for different standard versions e.g-std=c89
or-std=gnu17
or-std=gnu++11
, so might be worth inspecting, to see how you can tell whether you're compiling for C89, C99, or if you're using clang (#define __clang__
), etc"A little song, a little dance, a little seltzer down your pants" Chuckles the clown