Rewrote cmake. Added multiple target for both the td and exercices.

This commit is contained in:
Yohan Boujon 2023-09-25 08:59:27 +02:00
parent ef71402c94
commit a5e2182159
22 changed files with 50 additions and 21 deletions

View file

@ -1,25 +1,30 @@
cmake_minimum_required(VERSION 3.22)
# Global Variables
set(TARGET_NAME cpp_insa)
set(EXERCICE_TARGET exercice_insa)
set(TD_TARGET td_insa)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Set the etape value
set(ETAPE "1" CACHE STRING "Entrez un nombre d'etape a compiler.")
set(ETAPE "1" CACHE STRING "Entrez l'exercice a compiler.")
set(TD "1" CACHE STRING "Entrez le td a compiler.")
#Setting up the sources directory
file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/src/etape${ETAPE}/*.cpp")
# Etapes exercice
file(GLOB SOURCES_EXE "${PROJECT_SOURCE_DIR}/exercice/etape${ETAPE}/*.cpp")
add_executable(${EXERCICE_TARGET} ${SOURCES_EXE})
# cpp_insa Project (source, header)
add_executable(${TARGET_NAME} ${SOURCES})
# Adding the headers
target_include_directories(${TARGET_NAME} PUBLIC "include")
# TD
file(GLOB SOURCES_TD "${PROJECT_SOURCE_DIR}/td/td${TD}/src/*.cpp")
add_executable(${TD_TARGET} ${SOURCES_TD})
target_include_directories(${TD_TARGET} PUBLIC "${PROJECT_SOURCE_DIR}/td/td${ETAPE}/include")
# Adding compilation option depending on the OS
if(MSVC)
target_compile_options(${TARGET_NAME} PUBLIC /W3 /WX /DEBUG )
target_compile_options(${EXERCICE_TARGET} PUBLIC /W3 /WX /DEBUG )
target_compile_options(${TD_TARGET} PUBLIC /W3 /WX /DEBUG )
else()
target_compile_options(${TARGET_NAME} PUBLIC -Wall -Wextra -Wpedantic -Werror -lstdc++)
target_compile_options(${EXERCICE_TARGET} PUBLIC -Wall -Wextra -Wpedantic -Werror -lstdc++)
target_compile_options(${TD_TARGET} PUBLIC -Wall -Wextra -Wpedantic -Werror -lstdc++)
endif()

View file

@ -5,24 +5,25 @@
### Préparer la compilation
Pour compiler le projet je recommande CMake, mais il est possible d'utiliser directement 'g++'/'gcc'.
Une simple commande vous permet de préparer les fichiers dépendant de l'étape :
Une simple commande vous permet de préparer les fichiers dépendant de l'étape pour le td ou l'exercice :
```
mkdir build
cd build
```
```
cmake -DETAPE=2 ..
cmake -DETAPE=2 -DTD=1 ..
```
_Dans cet exemple, l'étape 2 sera construite._
_Dans cet exemple, l'étape 2 et le td 1 seront construits._
### Lancer la compilation
Il suffit ensuite de construire l'executable dans le dossier désiré
```
cmake --build .
cmake --build . --target <target_name>
```
>`<target_name>` étant soit `td_insa` ou `exercice_insa`
## Conseil pour compiler et executer efficacement
@ -30,10 +31,9 @@ Je vous suggère d'aller dans le dossier contenant l'executable dès que les deu
Pour cela rendez vous à la racine du projet après avoir crée et préparer le build :
```
./run.sh
./run.sh <target_name>
```
_Pour GNU GCC/G++ sur Linux_
```
run.bat
```
_Pour MSVC sur Windows_
>`<target_name>` étant soit `td_insa` ou `exercice_insa`
_le script Windows n'est plus fonctionnel pour le moment_

View file

@ -12,5 +12,6 @@ int main(void)
Point2D pointC;
std::cin >> pointC;
std::cout << "pointC entered: " << pointC << std::endl;
friendFunction(pointA);
return 0;
}

View file

@ -59,4 +59,9 @@ std::istream& operator>>(std::istream& stream, Point2D& point)
Point2D operator*(int value, const Point2D& point)
{
return {point._x*value, point._y*value};
}
void friendFunction(Point2D& obj)
{
std::cout << obj._x << std::endl;
}

View file

@ -16,6 +16,7 @@ class Point2D {
friend std::ostream& operator<<(std::ostream& stream, const Point2D& point);
friend std::istream& operator>>(std::istream& stream, Point2D& point);
friend Point2D operator*(int value, const Point2D& point);
friend void friendFunction(Point2D& obj);
protected:
float _x,_y;
};

14
run.sh
View file

@ -1,2 +1,12 @@
cmake --build build
./build/cpp_insa
#!/bin/bash
# check target name cmake
#return an error if not successful
if [ $# -eq 0 ]; then
echo "Usage: $0 <target_name>"
exit 1
fi
target_name="$1"
cmake --build build --target "$target_name"
"./build/$target_name"

7
td/td1/src/main.cpp Normal file
View file

@ -0,0 +1,7 @@
#include <iostream>
int main(void)
{
std::cout << "Hello world!" << std::endl;
return 0;
}