Build system output folder structuring - c

I want to make a public source library in C and I've been having a joyous time trying to work with both Makefiles and CMake. I like the simplicity of having one makefile per build partition but it's not cross-platform. I like the fact that CMake is cross-platform and although I hate the syntax types the language uses (I can get over that I guess..) it's the fact that when building, CMake floods my folders with a f*** tonne of cache files and I can't seem to change where they go. I would like to go with CMake since it seems to be more industry standard.
I like my builds in folders; Everything I care about in a seperate folder from all the build specific files that need to be generated. In visual studio I have this build structure and I would like to replicate it.
SolutionDir:
┝ Builds/
| ┝ Inter/ #For intermediate files
| | ┝ Debug/
| | | ┕ lib.o
| | ┕ Release/
| | ┕ lib.o
| ┝ Debug/ #For the debug build files
| | ┕ ProjectName/ ... .exe
| ┕ Release/ #For the release build files
| ┕ ProjectName/ ... .exe
┕ ProjectName/
┕ Source/
| lib.h
┕ lib.c
I cant even figure out how to make a sub directory in either systems for the build folder side, of course you can include sub directories for finding the source code so there must be a way? Any help would be greatly appreciated, I've been at this for too long now.

You can do whatever you want with makefiles, but since you ask about cmake, the only way to do it is to run the build from the build folder. In other words, you do this (assuming that you have SolutionDir/CMakeLists.txt):
cd SolutionDir
mkdir Builds
cd Builds
cmake ..
make -j8
(or whatever make command that you want). The Builds directory can be anywhere you want, it doesn't have to be within SolutionDir. You pass the directory containing the CMakeLists.txt file to cmake.

Related

Manually specifying include directory for Swift C module

I have a C library MyLib that I am trying to use in a Swift app. I want to use a local copy of the library, so I don't want to install it or add my bridge header to its files. I clone the library to Sources/ClibMyLib/MyLib so the package structure looks like this:
App
| Package.swift
| Sources/
| ClibMyLib/
| module.modulemap
| bridge.h
| MyLib/
| include/
| myLibHeader.h
| source/
| *.c
I specify bridge.h as the header in the module map:
module ClibMyLib {
umbrella header "bridge.h"
link "MyLib"
export *
}
The problem is that the include directory of MyLib is not visible to bridge.h So if bridge.h looks like this:
#include "myLibHeader.h"
it fails to build since it can't find myLibHeader.h. If I specify the path from bridge.h to the local copy like this:
#include "MyLib/include/myLibHeader.h"
then any transitive includes in myLibHeader.h still fail.
This would be very easy to solve in a C build, I would just add MyLib/include as an include directory. I haven't been able to find a way to do something similar in Swift - most guides I have found install the library and include from /usr/local/include/.
Are my only options to place bridge.h in the library's include directory or to install the headers to /usr/local/include? Adding bridge.h to the library is not ideal, the include directory file structure is more complicated than the simple example I gave. I just want to be able to manually specify an include directory.

change netbeans C project release and debug folder

I'm using Netbeans for C/C++ projects and I'd like to know how I can change the output directory for debug/release.
By default, the output path for a project is
<projectname>\dist\Debug\Cygwin_4.x-Windows\<projectname>.exe
I checked the project properties but I didn't found anything that made sense for me.
Thanks in advance!
You can change it here:
click on project -> Properties -> Build -> Linker there you can set the path at Output.
The default one uses variables for path, but you can set as you like.
Defualt explained:
${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/<Your Project Name>
^ ^ ^
| | |
Destination dir | Target platform
|
Configuration, eg. debug or release

Create a build environment for "C" project to dynamically select folders during compile time

Lets say my folder structure is something like this ..
+-- Application
|
+-- MICRO_CONTROLLER_1
|
+-- MICRO_CONTROLLER_2
|
+-- MICRO_CONTROLLER_3
and i have a compile switch ( SELECT_MICRO) set to #define SELECT_MICRO == MICRO_CONTROLLER_1 , then my project should build application with driver files in MICRO_CONTROLLER_1 , similarly if #define SELECT_MICRO == MICRO_CONTROLLER_2 , then application should build application with driver files in MICRO_CONTROLLER_2
Please let me know if there template to achieve the above.
You can export that particular path of the folder you want to build and supply the path to the executable. You can get further info. in this thread.
How I could add dir to $PATH in Makefile?
Or simply maintain different Makefiles to make different builds and use make -f to run that particular makefile.
I hope this is what you finally want to perform.
Typically you would define your pre-processor definitions to tell the pre-processor to include only, for instance, MICRO_CONTROLLER_1 blocks of code and ignore everything else.
Something like the following should suffice:
#if defined(MICRO_CONTROLLER_1)
// Block of code that is only available to MICRO_CONTROLLER_1
#elif defined(MICRO_CONTROLLER_2)
// ...
// All other microcontrollers you are supporting would follow this structure.
#endif
Then you would need to define MICRO_CONTROLLER_1. If you're using an IDE for development, there is typically a project option for pre-processor directives. This is where you would define MICRO_CONTROLLER_1. You could then create different "configurations" - one for each of the microcontrollers you are targeting.
This can only work if the directories have only include files. #define is a preprocessor directive. If the directories have source files, you need to solve it at the build system layer, not the preprocessor layer.
Assuming it's just include files, you'd just #include SELECT_MICRO # "Interface.h"

Getting link errors with CMake

I'm getting multiple definition link errors after conditionally compiling platform-specific code.
My project is laid out like this:
/
|__+ include/
| |__+ native/
| | |__ impl.h
| |
| |__ general.h
|
|__+ src/
|__+ native/
| |__ impl.linux.c
| |__ impl.win32.c
|
|__ general.c
At the top of the general.c file:
#if defined(LIBRARY_PLATFORM_LINUX)
#include "native/impl.linux.c"
#elsif defined(LIBRARY_PLATFORM_WIN32)
#include "native/impl.win32.c"
#endif
I set up introspection in CMake in order to detect the operating system and define the corresponding constants. The thing is, I didn't want to maintain one CMakeLists.txt file in every directory, so I simply globbed all the .c files as suggested in this answer:
file(GLOB_RECURSE LIBRARY_SOURCE_FILES "${PROJECT_SOURCE_DIR}/src/*.c")
Apparently, this is what is causing the problem. It seems to be compiling the code #included in general.c as well as the individual src/native/impl.*.c files.
CMakeFiles/lib.dir/src/native/impl.linux.c.o: In function `declared_in_impl_h':
impl.linux.c:(.text+0x0): multiple definition of `declared_in_impl_h'
CMakeFiles/lib.dir/src/general.c.o:general.c:(.text+0x0): first defined here
How can I untangle this situation?
The best practice for that sort of cross-platform situation is to create two libraries, one for linux and one for windows and stop doing conditional includes. Each platform only compiles and links the relevant library.
The recommended way to do that with cmake is to stop globbing and just include each file. There are some situations where it can get confused and not realize that it needs to recompile. You can make an argument that non-changing legacy code won't have that problem.
If you really want to avoid doing either of these things, I would put the included code in a header instead of a c file. You don't really want the include guards so that people don't get it confused for something that should be used like a regular header. Put a bunch of comments in the file to warn them off of said behavior as well.

Is there a C project Default Directory Layout?

I've always wanted to know if there is a default directory layout for C projects. You know, which folders should i put which files and such.
So I've downloaded lots of project's source codes on SourceForge and they were all different than each other.
Generally, I found more or less this structure:
/project (root project folder, has project name)
|
|____/bin (the final executable file)
|
|
|____/doc (project documentation)
| |
| |____/html (documentation on html)
| |
| |____/latex (documentation on latex)
|
|
|____/src (every source file, .c and .c)
| |
| |____/test (unit testing files)
|
|
|____/obj (where the generated .o files will be)
|
|
|____/lib (any library dependences)
|
|
|____BUGS (known bugs)
|
|____ChangeLog (list of changes and such)
|
|____COPYING (project license and warranty info)
|
|____Doxyfile (Doxygen instructions file)
|
|____INSTALL (install instructions)
| |
|____Makefile (make instructions file)
|
|____README (general readme of the project)
|
|____TODO (todo list)
Is there a default standard somewhere?
Edit: Sorry, really. I realised there are numerous similar questions for recommended C project directory files. But I've seen people say what they think is best. I'm looking for a standard, something that people usually follow.
Related Questions:
C - Starting a big project. File/Directory structure and names. Good example required
Folder structure for a C project
File and Folder structure of a App/Project based in C
Project Organization in C Best Practices
I would say "no", and your empirical evidence seems to support that.
I usually get confused right around when I need to decide between doc/ and docs/ ...
Well, there is “libabc” which is showcasing common practice.

Resources