From d512bb4dd234251a836f95224c76d8bea08e085a Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:31:51 +0300 Subject: [PATCH] begin working on process enumeration, add dummy target and adjust makefile for multiple targets --- Makefile | 38 ++++++++++++++++++++++++-------------- include/process.h | 15 +++++++++++++++ src/dummy_target.cpp | 16 ++++++++++++++++ src/main.cpp | 12 ++++++++++-- src/process.cpp | 6 ++++++ 5 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 include/process.h create mode 100644 src/dummy_target.cpp create mode 100644 src/process.cpp diff --git a/Makefile b/Makefile index ca1db35..a920eb1 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,22 @@ -INC_DIR = include -SRC_DIR = src -OBJ_DIR = obj -BIN_DIR = bin +INC_DIR = include +SRC_DIR = src +OBJ_DIR = obj +BIN_DIR = bin -CC = g++ -LD = ld -CXXFLAGS = -Wall -I$(INC_DIR) -LDFLAGS = +CC = g++ +LD = ld +CXXFLAGS = -Wall -I$(INC_DIR) +LDFLAGS = -BLACKJACK_SRC = main.cpp -BLACKJACK_OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.cpp,%.o,$(BLACKJACK_SRC))) -BLACKJACK_SRC := $(addprefix $(SRC_DIR)/,$(BLACKJACK_SRC)) -BLACKJACK_DEPS = +BLACKJACK_SRC = main.cpp process.cpp +BLACKJACK_OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.cpp,%.o,$(BLACKJACK_SRC))) +BLACKJACK_SRC := $(addprefix $(SRC_DIR)/,$(BLACKJACK_SRC)) +BLACKJACK_DEPS = process.h +BLACKJACK_DEPS := $(addprefix $(INC_DIR)/,$(BLACKJACK_DEPS)) + +DUMMY_TARGET_SRC = dummy_target.cpp +DUMMY_TARGET_OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.cpp,%.o,$(DUMMY_TARGET_SRC))) +DUMMY_TARGET_SRC := $(addprefix $(SRC_DIR)/,$(DUMMY_TARGET_SRC)) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(CC) $(CXXFLAGS) -c -o $@ $< @@ -19,13 +24,18 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp blackjack: $(BLACKJACK_OBJ) $(BLACKJACK_DEPS) $(CC) $(LDFLAGS) -o $(BIN_DIR)/$@ $(BLACKJACK_OBJ) +dummy_target: $(DUMMY_TARGET_OBJ) + $(CC) $(LDFLAGS) -o $(BIN_DIR)/$@ $(DUMMY_TARGET_OBJ) + .PHONY: all clean debug -all: blackjack +TARGETS = blackjack dummy_target + +all: $(TARGETS) debug: CXXFLAGS += -DDEBUG -g debug: LDFLAGS += -g -debug: all +debug: $(TARGETS) clean: rm $(OBJ_DIR)/*.o diff --git a/include/process.h b/include/process.h new file mode 100644 index 0000000..7524f6d --- /dev/null +++ b/include/process.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include + +class Process +{ +public: + Process(pid_t pid) : pid(pid) {} + + static std::optional FindByName(std::string name); +private: + pid_t pid; +}; \ No newline at end of file diff --git a/src/dummy_target.cpp b/src/dummy_target.cpp new file mode 100644 index 0000000..5d95cd6 --- /dev/null +++ b/src/dummy_target.cpp @@ -0,0 +1,16 @@ +#include +#include + +extern char *program_invocation_name; +extern char *program_invocation_short_name; + +int main() +{ + printf("pid: %d\n", getpid()); + printf("program_invocation_name: %s\n", program_invocation_name); + printf("program_invocation_short_name: %s\n", program_invocation_short_name); + + puts("waiting for any key..."); + getc(stdin); + return 0; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4c5c742..f2babeb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,15 @@ #include +#include "process.h" -int main() +int main(int argc, char** argv) { - std::cout << "Hello World" << std::endl; + auto proc = Process::FindByName("dummy_target"); + if (proc) proc = proc.value(); + else + { + fputs("process not found\n", stderr); + return 1; + } + return 0; } \ No newline at end of file diff --git a/src/process.cpp b/src/process.cpp new file mode 100644 index 0000000..1a808eb --- /dev/null +++ b/src/process.cpp @@ -0,0 +1,6 @@ +#include "process.h" + +std::optional Process::FindByName(std::string name) +{ + return {}; +}