์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

M1 ๋งฅ๋ถ Google test ํ™˜๊ฒฝ ๊ตฌ์ถ•

kite707 2023. 11. 16.

๐Ÿ“ŒC/C++๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ธ Google test๋ฅผ M1๋งฅ๋ถ์—์„œ ์„ธํŒ…ํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค.

 

๋ณดํ†ต WSL์— ๊น”์•„์„œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ M1, M2๋งฅ๋ถ์—์„œ๋Š” ์ž˜ ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ด์„œ docker๋ฅผ ์ด์šฉํ•ด ํ™˜๊ฒฝ์„ ์„ธํŒ…ํ–ˆ๋‹ค. ๋ช…๋ น์–ด๋“ค์€ iTerm์— ์ž…๋ ฅํ•˜๋ฉฐ ์ง„ํ–‰ํ–ˆ๋‹ค.

 

๋จผ์ € ์•„๋ž˜ ์ด๋ฏธ์ง€๋ฅผ pull ๋ฐ›๊ณ  ์ดํ›„ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‹คํ–‰ํ•œ๋‹ค.

https://hub.docker.com/r/srzzumix/googletest/

 

Docker

 

hub.docker.com

docker pull srzzumix/googletest

docker run -it -v /$(pwd):/code srzzumix/googletest sh

์—ฌ๊ธฐ๊นŒ์ง€ ์‹คํ–‰ํ–ˆ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด #์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ปค๋งจ๋“œ๋ผ์ธ์œผ๋กœ ์ง„์ž…ํ•˜๊ฒŒ ๋œ๋‹ค.

dockerDesktop ์•ฑ์—์„œ๋„ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‚˜๊ฐ€๋ ค๋ฉด exit ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋‹ค๋งŒ docker run -it -v /$(pwd):/code srzzumix/googletest sh๋ช…๋ น์–ด๋Š” ์ด๋ฏธ์ง€๋ฅผ ํ†ตํ•ด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งŒ๋“œ๋Š” ๋ช…๋ น์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ์˜ ์ปค๋งจ๋“œ๋ผ์ธ์œผ๋กœ ์ ‘๊ทผํ•˜๋ ค๋ฉด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด์ธ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์œ„ ๋ช…๋ น์–ด๋ฅผ ๊ณ„์† ์‚ฌ์šฉํ•˜๋ฉด ๊ณ„์†ํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒˆ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

docker exec -it [์ปจํ…Œ์ด๋„ˆ Id] /bin/sh

์ด์ œ ์„ค์น˜ํ•œ googleTest๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋„๋ก ํ•˜์ž. ์œ„ ๋ช…๋ น์–ด๋กœ ๋‹ค์‹œ ์ปจํ…Œ์ด๋„ˆ ์ปค๋งจ๋“œ๋ผ์ธ์— ์ ‘๊ทผํ•œ ๋’ค ํด๋”๋ฅผ ๋งŒ๋“ค๋„๋ก ํ•˜์ž.

mkdir my_project && cd my_project

๊ทธ๋ฆฌ๊ณ  my_project ํด๋”์— ์•„๋ž˜์™€ ๊ฐ™์ด ํŒŒ์ผ์„ ๋‘๊ฐœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ํŒŒ์ผ๋ช…๋„ ๋งž์ถฐ์•ผ ์ •์ƒ์ ์œผ๋กœ vi๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ คํ–ˆ์œผ๋‚˜ ์ž˜ ๋˜์ง€ ์•Š์•„์„œ ๋‚˜๋Š” vscode๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค.

 

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(my_project)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(
  hello_test
  hello_test.cc
)
target_link_libraries(
  hello_test
  GTest::gtest_main
)

include(GoogleTest)
gtest_discover_tests(hello_test)

hello_test.cc

#include <gtest/gtest.h>

// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {
  // Expect two strings not to be equal.
  EXPECT_STRNE("hello", "world");
  // Expect equality.
  EXPECT_EQ(7 * 6, 42);
}

 

์ด์ œ ๋‹ค์‹œ ํ„ฐ๋ฏธ๋„๋กœ ๋Œ์•„์™€์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

mkdir build && cd build

cmake ..  //CMakeLists.txt์— ์ •์˜๋œ ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ Makefile ํŒŒ์ผ ์ƒ์„ฑ

make //Makefile์‹คํ–‰

./hello_test //hello_testํŒŒ์ผ ์‹คํ–‰

์—ฌ๊ธฐ๊นŒ์ง€ ํ–ˆ์œผ๋ฉด build ํŒŒ์ผ ์•ˆ์— hello_test ์‹คํ–‰ ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ์„ํ…๋ฐ ์ด๊ฒƒ์„ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. googleTestํ™˜๊ฒฝ์ด ๋„์ปค์— ๊ตฌ์ถ•๋˜์–ด ์žˆ๊ธฐ์— iterm์—์„œ docker exec ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด bash๋ฅผ ์—ฐ ๋’ค ์‹คํ–‰ํ•ด๋„ ๋˜๊ณ  ๋” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” vscode์—์„œ docker extension์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

 

docker extension์„ ๊น”๋ฉด ์™ผ์ชฝ์— ๋„์ปค ๋กœ๊ทธ๊ฐ€ ์ƒ๊ธฐ๋Š”๋ฐ ์ปจํ…Œ์ด๋„ˆ ์ค‘ ๊ตฌ๊ธ€ ํ…Œ์ŠคํŠธ๊ฐ€ ๊น”๋ฆฐ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์šฐํด๋ฆญํ•˜๋ฉด attach shell๋ฉ”๋‰ด๊ฐ€ ๋‚˜์˜จ๋‹ค.

ํ•ด๋‹น ์‰˜์—์„œ ์œ„์—์„œ ๋งŒ๋“  buildํด๋”๋กœ ์ด๋™ํ•œ ๋’ค hello_testํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋ฉด ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

cd my_project/build //์œ„์—์„œ ๋งŒ๋“  ํด๋”๋กœ ์ด๋™

./hello_test. //hello_testํŒŒ์ผ ์‹คํ–‰

์ด์ œ testํŒŒ์ผ์ด ๋ฐ”๋€Œ์—ˆ๋‹ค๋ฉด build ํด๋”์—์„œ make๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด ์ค€ ๋’ค ๋‹ค์‹œ hello_testํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด hello_test.cc์˜ ๋‚ด์šฉ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ–ˆ๋‹ค.

#include <gtest/gtest.h>

// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {
  // Expect two strings not to be equal.
  EXPECT_STRNE("hello", "world");
  // Expect equality.
  EXPECT_EQ(7 * 6, 42);

  // [์ถ”๊ฐ€๋œ ๋‚ด์šฉ] ์‹คํŒจํ•  ํ…Œ์ŠคํŠธ
  EXPECT_EQ(7 * 6, 50);
}

 

์ดํ›„ docker์˜ bash์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋“ค์„ ์‹คํ–‰ํ•ด์ฃผ์—ˆ๋‹ค.

//buildํด๋”์—์„œ

make //makeํŒŒ์ผ์— ๋ช…์‹œ๋œ ๋‚ด์šฉ์— ๋”ฐ๋ผ ์žฌ์ปดํŒŒ์ผ

./hello_test //์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ง„ hello_testํŒŒ์ผ ์ƒ์„ฑ

์—ฌ๊ธฐ๊นŒ์ง€ ์„ฑ๊ณตํ–ˆ๋‹ค๋ฉด googleTestํ™˜๊ฒฝ ๊ตฌ์ถ•์ด ์™„๋ฃŒ๋œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์œ„์—์„œ ์‚ฌ์šฉํ•œ cmake์™€ make์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ  ๊ธ€์„ ๋งˆ์น˜๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

cmake๋ช…๋ น์€ Makefile์„ ์ƒ์„ฑํ•˜๋Š” ๋ช…๋ น์–ด์ด๊ณ  makeํŒŒ์ผ์€ Makefile์— ์ •์˜๋œ ๋‚ด์šฉ์„ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด์ด๋‹ค.

 

Makefile์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜์ž๋ฉด ํŒŒ์ผ๊ฐ„์˜ ์˜์กด์„ฑ์„ ์ •์˜ํ•ด ๋†“์€ ํŒŒ์ผ์ด๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ํŒŒ์ผ ๊ตฌ์กฐ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.

์ด ์ƒํ™ฉ์—์„œ world.c๊ฐ€ ์ˆ˜์ •๋˜์—ˆ๋‹ค๊ณ  ํ•˜๋ฉด ์šฐ๋ฆฌ๋Š” world.c์™€ world.h๋ฅผ ์ปดํŒŒ์ผํ•˜์—ฌ world.o๋ฅผ ๋งŒ๋“ค๊ณ  ๋‹ค์‹œ hello1.o์™€ world.o๋ฅผ ์ปดํŒŒ์ผํ•˜์—ฌ hello_world์‹คํ–‰ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ๋งŒ์ผ ํŒŒ์ผ ๊ตฌ์กฐ๊ฐ€ ๋” ๋ณต์žกํ•˜๋‹ค๋ฉด? ๋” ์—ฌ๋Ÿฌ๋ฒˆ ์ปดํŒŒ์ผ ๋ช…๋ น์–ด๋ฅผ ์ณ์•ผ ํ•  ๊ฒƒ์ด๋‹ค.

 

์ด๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด Makefile์ด๋‹ค. Makefile์—๋Š” ๊ฐ ํŒŒ์ผ์ด ์–ด๋–ค ํŒŒ์ผ์— ์˜์กดํ•˜๊ณ ์žˆ๋Š”์ง€ ์ •๋ณด๊ฐ€ ๊ธฐ๋ก๋˜์–ด์žˆ์–ด์„œ make๋ช…๋ น์–ด ํ•œ ์ค„์ด๋ฉด ์œ„์—์„œ ํ•ด์•ผ ํ•  ์ž‘์—…์„ ๋ชจ๋‘ ํ•ด์ค€๋‹ค. ๋งŒ์ผ ์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์ด๋ผ๋ฉด make๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ world.c์™€ world.h๋ฅผ ์ปดํŒŒ์ผํ•˜์—ฌ world.o๋ฅผ ๋งŒ๋“ค๊ณ  ๋‹ค์‹œ hello1.o์™€ world.o๋ฅผ ์ปดํŒŒ์ผํ•˜์—ฌ ์ƒˆ๋กœ์šด hello_world ์‹คํ–‰ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์ค„ ๊ฒƒ์ด๋‹ค.

๋Œ“๊ธ€