• Forum
  • Lounge
  • The journey to fun continues...with a Ma

 
The journey to fun continues...with a Makefile...and Linux

closed account (z05DSL3A)
Hello again,

This might be a bit of a ramble but feel free to chip in with any advice if I forget to specifically ask. :0)

So back in January I said I was coming back to C and C++, relearning and refreshing[1]. I have got my hands on a few books[2] that I'm going to be using for C++ refresher/study. I prefer good books to online resources but I have bookmarked a lot of C++ conference videos on YouTube.

For a long time now I have been Windows only (apart from a bit of tinkering with Raspberry Pi, BeagleBone,and the like) but I have got a bit...irked by the heavy weight nature of Visual Studio enterprise that I normally use. I was after a quick and simple way to roll out, edit, compile and test code and I was fighting Windows every step of the way. Step in a rather knackered old laptop with an AMD E1-1500 APU and 8 GB of RAM. With a fresh install Ubuntu 20.04 LTS and a bit of fettling I have a serviceable dev box...at least for now. I'm still remembering my way round developing on a *nix box and want to stick with command line tooling. Still finding my feet with vi/nvim

So with that in mind, I have started to create a basic directory that I can easily clone to start new toy projects. For this I decided to use make so I can keep things consistent and tidy.
.
├── build
│   ├── apps
│   │   └── program
│   └── objects
│       └── source
│           ├── main.d
│           └── main.o
├── .clang-format
├── .clang-tidy
├── include
│   └── main.hpp
├── Makefile
└── source
    └── main.cpp

Here is the makefile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
TARGET    := program
CXX       := -clang++
CXXFLAGS  := -pedantic-errors -Wall -Wextra -Werror
LDFLAGS   := -L/usr/lib -lstdc++ -lm
BUILD_DIR := ./build
OBJ_DIR   := $(BUILD_DIR)/objects
APP_DIR   := $(BUILD_DIR)/apps
INCLUDE   := -Iinclude/
SRC       :=  $(wildcard source/*.cpp)

OBJECTS   := $(SRC:%.cpp=$(OBJ_DIR)/%.o)
DEPENDENCIES := $(OBJECTS:.o=.d)

all: build $(APP_DIR)/$(TARGET)

$(OBJ_DIR)/%.o: %.cpp
	@mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -MMD -o $@

$(APP_DIR)/$(TARGET): $(OBJECTS)
	@mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) -o $(APP_DIR)/$(TARGET) $^ $(LDFLAGS)

-include $(DEPENDENCIES)

.PHONY: all build clean debug release info

build:
	@mkdir -p $(APP_DIR)
	@mkdir -p $(OBJ_DIR)

debug: CXXFLAGS += -DDEBUG -g
debug: all

release: CXXFLAGS += -O2
release: all

clean:
	-@rm -rvf $(OBJ_DIR)/*
	-@rm -rvf $(APP_DIR)/*

info:
	@echo "[*] Application dir: ${APP_DIR}"
	@echo "[*] Object dir:      ${OBJ_DIR}"
	@echo "[*] Sources:         ${SOURCE}"
	@echo "[*] Objects:         ${OBJECTS}"
	@echo "[*] Dependencies:    ${DEPENDENCIES}"
 */

I think it's okay, any thoughts?

I want to add clang-format and clang-tidy to the mix and maybe a test framework. I'm also trying to figure out how best to get git involved.

As I said this is for quick toy/practice projects, cmake will be used for larger/cross-platform things.



_______________________________________________________________________
[1]https://www.cplusplus.com/forum/lounge/275158/#msg1187467

[2]
The C++ Programming Language by Stroustrup Bjarne
C++ Standard Library, The: A Tutorial and Reference by Nicolai Josuttis
C++ Templates: The Complete Guide by David Vandevoorde , Nicolai Josuttis, et al.
C++17 - The Complete Guide by Nicolai M. Josuttis
Last edited on
Also have a look at the on-line tutorial https://www.learncpp.com/
closed account (z05DSL3A)
-----===# 1 #===-----------------------------------------------------------------

Thanks seeplus

-----===# 2 #===-----------------------------------------------------------------

It's funny how my mind can change over night. After posting the above, I went looking for a C++ logging library, got to love a good looger, and went down a rabbit hole, I found Conan[1] mentioned a lot, so I dutifully took a detour to investigate.

Conan looks quite nice and seems to play well with CMake...now I remember CMake to be a bit of a handful. I had a love hate relationship with it. I knew I'd end up back in it's embrace and I was in no hurry but talk of modern cmake got my interest piqued. It was 2am and the match sticks holding my eyes open where under too much strain, off to bed.

This morning I got myself a copy of Professional CMake: A Practical Guide[2] by Craig Scott...this is not the CMake I remember...which makes me happy.

I guess the next big monster I'll face will be the debugger...I've been totally spoilt by Microsoft here.

Edit: PS spdlog[3] is the C++ logging library I think I'll try.
____________________________________________________________
[1] Conan the C/C++ Package Manager [ https://conan.io/ ]
[2] https://crascit.com/professional-cmake/
[3] https://github.com/gabime/spdlog
Last edited on
Did you know you can do command-line compiling with Visual Studio, bypassing the IDE?

Walkthrough: Compiling a Native C++ Program on the Command Line https://docs.microsoft.com/en-us/cpp/build/walkthrough-compiling-a-native-cpp-program-on-the-command-line?view=msvc-160

Walkthrough: Compile a C program on the command line
https://docs.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=msvc-160

I haven't really tried command-line compiling, I'm kinda lazy and spoilt by the IDE.
closed account (z05DSL3A)
Hi Furry Guy,

I do know you can do command-line compiling with the VS toolchain, I don't feel 'comfortable' on a Windows command line. I don't know if this will make sense or not but feel that everything about Windows is pushing me to use 'windows' and everything about Linux is pushing me to use the command line. I really don't like the Linux windows interface.

The reason for setting up Linux and using the laptop is one of logistics. My main box is unavailable large parts of the day as my partner is working from home currently (this also give bandwidth issues on the network). I had the old laptop with Windows 10 for testing software, it's good to run your .net code on a low spec box. As a low spec box it isn't up to running VS. I played around with a few ideas to get an environment that I liked and settled on Windows Subsystem for Linux (WSL). The next logical step was to remove Windows from the equation as it was only being used to host Linux.
Grey Wolf, I thought you might have already known, but since posts are read by other people who likely won't know what VS can do I thought I might be helpful to them.

Your OP gave me some things to investigate and ponder about a low spec programming environment. Being a programming hobbyist makes it easy for me to go out and root around in things I find interesting without restraint of a course curriculum or a boss/coworker nagging at me.

Cheers, I hope you get everything sorted out and things work to your satisfaction. :)
closed account (z05DSL3A)
Furry,

I think my reasoning for posting about this was to try and spark some discussion about the development environment and tools. I'm interested in if people use sanitisers, linters, static analysis, etc. as part of their regular build cycle, what tools and rules they use...

Anyone,

chime in. Share your thought on tools, favourite libraries, experiences.
I routinely use LLVM's UBSan https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
(I like it because it runs quite fast and does not mangle the memory layout, abi etc of the program)
I use Macromates' TextMate application to write and edit code, and LLVM's clang++ for compiling.

This is partly out of necessity, because VS doesn't work on my Mac, I don't have Xcode, and mainly, from what I've seen of IDE's, they look overly complicated for what I do (just dinking around). Terminal also has LLVM's LLDB debugger built-in so I use that for debugging.

Also, it looks really cool when I put Terminal on fullscreen, and use the Homebrew profile, it looks like something right out of the movie Wargames. Really impresses my friends at work, lol ;)

It's pretty easy to use though, it just uses a Bash CLI, and you can do practically anything with it if you know how (I actually know a guy who does literally all of his computer work from a Unix command-line).
Last edited on
Topic archived. No new replies allowed.