'#error' statement in header file not working - c

I have created a .c and a .h file with the same name. The .c file includes the header file and a main.c file includes both.
I want to put this statement in the header:
#ifndef TIMEOUT
#error TIMEOUT not set
#endif
To force the user to specify a TIMEOUT when compiling. The problem is that CLion says gives a red line under 'error' and says 'TIMEOUT not set'.
The weird thing is that up until now all my projects have used this. The difference is that the variables behind '#ifndef' and '#define' etc. have become dark green, whereas in my previous projects these were always grey.
Have I changed a setting to something not correct? Making a new project does not solve this problem.
Let me know if I should add screenshots, thanks in advance!

Related

Include a file in C with VsCodium and WSL

I'm trying to include a custom file into my program in C, and it compiles ok. However, VsCodium keeps complaining about "Squiggles" and "includePath" (don't know what that means), actually, I don't know precisely how to configure the ".vscode/c_cpp_properties.json" file as I am using WSL to compile the code and VsCodium is installed on Windows.
Here is the line:
#include "./types.h"
// #include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (\\wsl$\Arch\home\{*}\{*}\{*}.c).C/C++(1696)
// cannot open source file "./types.h"
What should I do? It's a little annoying although it's working
Go to File->Preferences->Settings.
Optional: Switch to the Workspace tab (if you only want this to take effect on the current workspace).
In the search box, type in c_cpp include path or something similar.
Find the C_Cpp->Default: Include path field, then click Add item.
Enter your include path which contains the file you want to include.

How to include a folder of libraries in C?

I'm trying to include a folder that contains a combination of around 60 .h and .hpp files. This folder contains libraries for programming robots with a Wallaby (a mini-computer-like device) for Botball competition. include is located in the same place as main.c (inside code). Up until now, this is what my header for including libraries looks like:
#include "../code/include/accel.h"
Just like accel.h, I have 60 other .h and .hpp files inside include. So, coming to my question, do I need to type out all the 60 header lines? or is there a way to include the include folder.
I'm using Clion for this project, if I can't include the folder itself, does anyone know of a shortcut in Clion to include all the files in include.
I was also thinking of using some sort of placeholder for the folder name and only specify the file type. So, for example: #include "../code/include/(generic placeholder name).h". I have no clue if something like this exists.
I would also request you to keep in mind that I'm a beginner to programming, so please keep your answers simple.
This is just for some extra info:
The Wallaby is a mini computer to which you would connect your sensors, motors, servos and cameras in order to control a robot for the Botball competition. Usually, one can connect to the Wallaby either via Wifi Direct or a cable and write programs on it directly through an online interface (not entirely sure of the word for it, but you just type in an IP address in your browser and it brings up an interface where you can make projects and code). All the code written in that interface saves directly onto the Wallaby. Here the default include statement is #include <kipr/botball.h>, so I'm assuming that botball.h (which is located on the Wallaby's storage) has all those 60 libraries consolidated in it. I got the include folder that I'm using from GitHub. This link was provided to me by one of the Botball organisers. So the main point in me trying to download the library is so that I can write and successfully compile code even when I'm not connected to the Wallaby. Hope this provides some relevant context.
Thank you for your answers!
What I'd do is
Create (maybe with scripting tools or a specific program) a "all.h" file which includes all the other header files
#ifndef ALL_INCLUDED
#define ALL_INCLUDED
#include "accel.h"
#include "bccel.h"
//...
#include "zccel.h"
#endif
Include "all.h" in your main file
#include "../code/include/all.h"
You can create "all.h" automatically every time you build your code.
CLion is an IDE for Clang and GCC. These compilers are instructed to search paths for include files by specifying -I<path> command line arguments. Any number may be specified, and they are searched in the order given, and the first match found is the file that gets included.
I am not familiar with CLion specifically but no doubt it has a dialog somewhere where you can set header file search paths.
Edit: It seems that CLion may not make this so straightforward. I understand that you have to add then via CMake: https://cmake.org/cmake/help/v3.0/command/include_directories.html#command:include_directories, but after that, the IDE will not recognise the header in the editor and will warn you of unrecognised files and will not provide code comprehension features. I believe it will build nonetheless.

How to predefine header file path in a project

I am trying to use the following method to include a project header file:
#include FILE_PATH
Where FILE_PATH is defined as the file to be included.
The project compiles without errors if FILE_PATH is include as:
#define FILE_PATH "hal/micro/config.h"
#include FILE_PATH
But if FILE_PATH is pre-defined as a compiler define option inside the project options, then building the project returns the following error:
Error #13: Expected a file name
The development software being used is Code Composer Studio version 6.
What am I missing here to pre-define the header file path in a project?
Additional Details:
I am in the process of converting a working project from the IAR embedded workbench IDE to Code Composer Studio. The Pre-define NAME (--define, -D) shown in the picture below are mostly identical to how they were in the IAR project.
The pre-define name boxed in red is currently the cause of the error, but this could occur with any of the other defines with file pathnames.
I have tried the suggestion of using the #ifdef statement to at least verify that PLATFORM_HEADER is actually defined and it does seem to be defined. I also checked for typos and there doesn't appear to be any noticeable typos.
The key reason for wanting to go with the pre-defined macro approach is to avoid individually making changes to numerous files affected by this error.
I still have not yet tried a command line compile, since I need to reference the manual on how to do so, but I will try as soon as I figure it out.
#StenSoft wrote:
The IDE does not correctly escape the parameters. You should escape the quotes. You can also try placing PLATFORM_HEADER somewhere in the code and see what the compiler would tell you it sees.

How to exclude some of the "unable to open include file *.h" errors in pclint

I am using PC lint in my project. My project is compatible to build in both windows and linux. So i have used windows header(visualstudio) files and linux header files(gcc) in my project. I am running pclint entirely for all the file. Its giving error message
Unable to open include file *.h
I dont want to suppress this error in std.lnt file and i dont want to add -elint errorcode before the include statement. Please suggest me is there any way to suppress particualar headerfiles in std.lnt file.
I am assuming that you don't really get message
Unable to open include file *.h
but are really getting message
Unable to open include file fred.h
for some file fred.h.
If I am correct, then add these two lines to std.lnt:
-efile(322,fred.h)
-efile(7,fred.h)
Protect the relevant includes with platform-dependant preprocessor symbols:
#if defined PLATFORM_PC
#include <whatever/is/needed.h>
#else if defined PLATFORM_POSIX
#include <stdio.h>
#endif
Then make sure you define PLATFORM_PC when checking the code with PC-Lint, so that it never sees the includes for the platform it doesn't understand.

Auto defines in C editors... Why?

When Eclipse creates a new file (.c or .h file) in a C project the editor always auto creates a #define at the top of the file like this: If the file is named 'myCFile.c' there will be a #define at the start of the file like this
#ifndef MYCFILE_C_
#define MYCFILE_C_
I have seen other editors do this as well (Codewright and SlikEdit I think).
The #defines don't seem to do anything for the editor as I can just delete them without any problem, and I can't think of a reason why I would want to use them. Does anyone know why they are there?
It's to guard against multiple definitions.
Sometimes people include a whole .c file in other .c files (or even .h files), so it has the exact same purpose of preventing an include file from getting included multiple times and the compiler spitting out multiple definition errors.
It is strange, though, that it would be the default behavior of an editor to put this in anything but a .h file. This would be a rarely needed feature.
A more modern version of this is to use:
#pragma once
It is quite unusual to see this in a .c file, normally it is in the header files only.
I think it's a throwback of C include issues, where multiple copies of the source would get included - unless you are meticulous with include chains (One file includes n others).
Checking if a symbol is defined and including only if the symbol is defined - was a way out of this.

Resources