Copyright (C) 2024 @github.com:HungNguyen501
All rights reserved.
Agenda
1. Prerequisites
2. How to configure C++ file by Cmake
3. How to configure Bazel
- Coding convention: Google C++ Style Guide
- Gcc version:
$ g++ --version
g++ (Ubuntu 13.2.0-23ubuntu4) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- Cmake version:
$ cmake --version
cmake version 3.29.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
- Bazel version:
$ bazel --version
bazel 7.2.1
- Install Cmake components:
$ make install
See output
-- Configuring done (0.1s)
-- Generating done (0.1s)
-- Build files have been written to: /home/mrroot501/Projects/Cpp-Core/configurations
gmake[1]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations'
gmake[2]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations'
gmake[3]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations'
gmake[4]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations'
gmake[4]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations'
gmake[4]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations'
[ 10%] Performing update step for 'googletest'
[ 20%] No patch step for 'googletest'
[ 30%] Performing configure step for 'googletest'
-- Configuring done (0.1s)
-- Generating done (0.0s)
-- Build files have been written to: /home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build
[ 40%] Performing build step for 'googletest'
gmake[5]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[6]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
[ 25%] Built target gtest
gmake[7]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
[ 50%] Built target gmock
gmake[7]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
[ 75%] Built target gmock_main
gmake[7]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
[100%] Built target gtest_main
gmake[6]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[5]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
[ 50%] Performing install step for 'googletest'
gmake[5]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[6]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
[ 25%] Built target gtest
gmake[7]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
[ 50%] Built target gmock
gmake[7]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
[ 75%] Built target gmock_main
gmake[7]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
gmake[7]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
[100%] Built target gtest_main
gmake[6]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations/lib/src/googletest-build'
Install the project...
-- Install configuration: ""
-- Up-to-date: /home/mrroot501/Projects/Cpp-Core/configurations/lib/installed/include
[ 60%] Completed 'googletest'
gmake[4]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations'
[ 80%] Built target googletest
gmake[4]: Entering directory '/home/mrroot501/Projects/Cpp-Core/configurations'
gmake[4]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations'
[100%] Built target hello_world
gmake[3]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations'
gmake[2]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations'
gmake[1]: Leaving directory '/home/mrroot501/Projects/Cpp-Core/configurations'
Hello World!
bazel 7.3.1
g++ (Ubuntu 13.2.0-23ubuntu4) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- Add
main.cpp
file toconfigurations/CMakeLists.txt
:
$ vim configurations/CMakeLists.txt
: '
# Add definition for library
add_library(lib_name path_to_files)
# Add targets for main program
add_executable(executable_file path_to_file)
target_link_libraries(executable_file lib_name)
# Add targets for testing
add_executable(test_name path_to_test_file)
target_link_libraries(test_name gtest Threads::Threads)
'
- List all executable targets in Cmake:
$ make list
: '
leetcode_count_number_of_consistent_strings_main
merge_intervals_main
find_an_element_in_array_sum_of_left_equal_sum_of_right_main
test_extra_long_factorial
'
- Build target in Cmake:
$ make cmake_build t={target}
- Run an executable in cmake:
$ make cmake_run t={target}