Force C compiler for extension capital C in Eclipse CDT - c

I'm having legacy Makefiles and would like to transfer them into Eclipse CDT Projects. Coming from windows, they have mixed .c as well as .C as extensions for C sources (while eclipse normally would treat capital ".C" as c++ files).
As they are checked into source control I'd like to avoid the hassle of renaming the source files.
As the files are linked into the workspace, I tried renaming the link to .c (leaving the target/original file as ".C"). CDT won't care and invoke the C++ compiler. Even worse, when I right-click on the file I get the settings for (only) the C compiler but what I change is irrelevant because still the C++ compiler with the project wide C++ settings is invoked.
Now, if there are only C files in the project I can set the project C++ settings under "Miscellaneous" to use -xc -std=c99 etc. and effectively force the g++ to behave like a proper C compiler - Which is not really beautiful and completely fails when there are other real .cpp files to be compiled as C++ within the same project.
Also, using project settings under C/C++ General/File Types and setting ".c" and ".C" to "C source file" doesn't seem to bother the CDT...
Am I overlooking something? Is this far outside the scope of CDT?

Related

How can I use my C library as others like stdio

I recently made a small library in C, and I wanted to put it together with the standard libraries so I don't have to always copy the files for each new project.
Where do I have to put it so I can import it like the standard libraries?
Compiler : MinGW
OS: Windows
You need to create a library, but you don't necessarily need to put it in the same place as MinGW's standard libraries (in fact I think that's a bad idea).
It is better to put your own library/libraries in specific place and then use the -I compiler flag to tell the compiler where to find the header files (.h, .hpp, .hh) and the -L linker flag to tell the linker where to find the library archives (.a, .dll.a). If you have .dll files you should make sure they are in your PATH environment variable when you run your .exe or make sure the .dll files are copied in the same folder as your .exe.
If you use an IDE (e.g. Code::Blocks or Visual Studio Code) you can set these flags in the global IDE compiler/linker settings so you won't have to add the flags for each new project.
Then when building a project that uses your library you will need to add the -l flag with the library name to your linker flags, but without the lib prefix and without the extension (e.g. to use libmystuff.a/libmystuff.dll.a specify linker flag -lmystuff). The use of the -static flag will tell the linker to use the static library instead of the shared library if both are available.
I have created a minimal example library at https://github.com/brechtsanders/ci-test to illustrate on how to create a library that can be build both as static and shared (DLL) library on Windows, but the same code also compiles on
macOS and Linux.
If you don't use build tools like Make or CMake and want do the steps manually they would look like this for a static library:
gcc -c -o mystuff.o mystuff.c
ar cr libmystuff.a mystuff.c
To distribute the library in binary form you should distribute your header files (.h) and the library archive files (.a).

Make clangd aware of macros given from the compiler

I have two executables that are build from the same source (a client and a server) and they're built with the compile options -D CLIENT=0 -D SERVER=1 for the server and -D CLIENT=1 -D SERVER=0 for the client. If I do something like
if (CLIENT) {
// Client specific code
}
clangd complains that CLIENT is not defined. Is there a way to make clangd aware of those macros? (The code compiles just fine, the errors are from clangd, not the compiler)
Is there a way to make clangd aware of those macros?
From getting started with clangd:
Project setup
To understand source code in your project, clangd needs to know the
build flags. (This is just a fact of life in C++, source files are not
self-contained.)
By default, clangd will assume that source code is built as clang
some_file.cc, and you’ll probably get spurious errors about missing
#included files, etc. There are a couple of ways to fix this.
compile_commands.json
compile_commands.json file provides compile commands for all source
files in the project. This file is usually generated by the build
system, or tools integrated with the build system. Clangd will look
for this file in the parent directories of the files you edit. Other
tools can also generate this file. See the compile_commands.json
specification.
compile_commands.json is typically generated with CMake build system, but more build systems try to generate it.
I would suggest moving your project to CMake, in the process you will learn this tool that will definitely help you in further C-ish development.
compile_flags.txt
If all files in a project use the same build flags, you can put those
flags, one flag per line, in compile_flags.txt in your source root.
Clangd will assume the compile command is clang $FLAGS some_file.cc.
Creating this file by hand is a reasonable place to start if your
project is quite simple.
If not moving to cmake, create a compile_flags.txt file with the content for example like the following, and clangd should pick this file up:
-DCLIENT=1
-DSERVER=1

Adding standard C library files to Atmel Studio

I have Atmel Studio installed on my system, but when I want to use for example time.h of the stdio, it tells me the library does not exist!
So I downloaded this library:
http://download.savannah.gnu.org/releases/avr-libc/
Now, how can I include the whole library in my project?
After extracting, there are individual .h files and .c files that I can add in libc and include folders, but I'd like to add them all to my project (at least all of stdio, time, string...).
Thanks

How to make ARM Eclipse for DS-5 aware of compiler predefined macros?

I am using ARM Eclispe for DS-5 environment to maintain and build a command-line (makefile based) project. Using the Eclipse's Indexer to analyze the code for intelligent browsing and editing, it looks like it does not recognize the underlying ARM C compiler 5 predefined macros. For example, if I have in my code:
#warning "XXXXXXXX Im here 1 XXXXXXX"
#ifdef __arm__
#warning "XXXXXXXX Im here 2 XXXXXXX"
#endif
then when compiling the module I see the first and second warnings (the compiler is aware of its own __arm__ macro, of-course). However, in the editor, the code in the #ifdef block is grayed out. Consequently, I have hundreds of false error indications in the Problems view.
How can I make the environment aware of the compiler built-in settings?
* Using ARM DS-5 version 5.20, and armcc version 5.05
You can set Eclipse C/C++ Project properties: Preprocessor Include Paths, Macros. Adding macros or preprocessor file helps to understand the compiler predefines/macros to eclipse.
Note that the entries could be set also on an individual resource, such as file or folder. Open file or folder properties to inspect resource-specific entries. The entries on a folder or a project will apply to all subfolders and C/C++ files under it - unless overridden on a lower level.
for more information you can check this link
Setting Up Include Paths and Macros for C/C++ Indexer

How to create a makefile to use the mpir and mpfr libraries

I've recently written a C program which uses the public-domain mpir and mpfr libraries. I've been compiling it on Windows, using the Microsoft Visual C++ 10.0 compiler. To get that to work, I had to do the following:
Download / build the mpir and mpfr libraries from http://gladman.plushost.co.uk/oldsite/computing/gmp4win.php
Move the files mpir.h and mpfr.h into the Include directory for the Microsoft compiler
Move the files mpir.lib, mpir.pdb, mpfr.lib and mpfr.pdb into the lib directory for the Microsoft compiler
#include mpir.h and mpfr.h in the program
Compile using cl <..module names..> /link mpir.lib mpfr.lib
I now want to send the source / header files I've written to someone else, along with a makefile that they can use to compile the code and check that it works. Since that person won't have the mpir / mpfr libraries installed, and might not be using the same compiler, I'm not quite sure how to do this.
Here is what I can do:
Send them the binaries mpir.lib, mpir.pdb, mpfr.lib and mpfr.pdb as well as the source / header files.
Here is what I can't do:
Send them my entire Microsoft Visual C++ 10.0 setup
Ask them to stick files in their Include and lib directories (unless there's no other way)
Ideally, I should be able to send them the source/header files, together with the pertinent mpir/mpfr binaries, and a makefile which they can then run to build the program.
Thanks in advance for your help!
Why on earth are you adding those files to your compiler installation path?? The compiler has command line options for specifying search paths.
For instance,
cl /I"path/to/mpfr/header" <...filenames...> /link /LIBPATH:"path/to/mpfr/lib" mpir.lib mpfr.lib
You should only have to send your source code, mpir.h, mpir.lib, mpfr.h and mpfr.lib. The PDB files contain debugging information, and are not necessary for compilation.
Also, I don't know how to create a makefile, but a simple batch file with the command above should suffice for something so simple.

Resources