• 카테고리

    질문 & 답변
  • 세부 분야

    개발 도구

  • 해결 여부

    해결됨

Xcode 빌드시스템 에서 빌드가 잘 되지 않는 문제가 있습니다.

23.10.27 15:15 작성 23.10.27 15:17 수정 조회수 435

0

여러가지 조사를 해봤지만 해결되는것이 없어서 도움을 요청해봅니다..

일단 Xcode는 설치가 되어 있고, Xcode 버젼은 다음과 같습니다.

xcode-select -p
> /Applications/Xcode.app/Contents/Developer

xcodebuild -version
Xcode 15.0.1
Build version 15A507

작업 위치는 외장 디스크를 터미널로 접근해서 작업을 했습니다.

ShellScript로 CMake 세팅을 했고요

bash ~/CMakeInitTemplate.sh Phello-cmake 를 실행하면

#!/bin/bash

# 명령어 결과물을 변수로 나타내기
CurPath=$(pwd)
ProjectName="$1"
# 현재 주소를 사용해서 concatenate하기

if [ -z "$ProjectName" ]; then
    echo "Type ProjectName by argument"
    echo "Like bash ~/CMakeInitTemplate [ProjectName]"
    exit 0
fi

mkdir "${ProjectName}"
cd "${ProjectName}" || exit

# 파일이 있는지 없는지 확인하기
    # https://co-no.tistory.com/109
    # if [ -e "$FILE" ]; then
    #     echo "File Exists"
    # else
    #     echo "No Matching FIle"
    # fi

# 파일 쓰기
# https://stackoverflow.com/questions/11162406/open-and-write-data-to-text-file-using-bash
Create_CMakeLists_Text() {
    touch CMakeLists.txt
    echo "# 최소 CMkae버젼 요구사항 명시
cmake_minimum_required(VERSION 3.0.0)

# 프로젝트 이름 지정
project(${ProjectName})

# 타겟 프로그램 정의
add_executable(${ProjectName} src/main.cpp)

# 조건문
if(BUILD_TESTING)
    # 메세지 출력
    message('Hello Test')
endif()" >> CMakeLists.txt
}

# 인프런 삼각형 GNU 폴더 구조
Set_CMake_FolderStructure() {
    mkdir bin       # 1. 동적 라이브러리와 실행파일   : bin
    mkdir data      # 2. 데이터와 에셋    : data
    mkdir demo      # 3. 데모 : demo
    mkdir doc       # 4. 문서 : doc
    mkdir include   # 5. 헤더파일 : include 
    mkdir lib       # 6. 라이브러리   : lib
    mkdir src       # 7. 소스파일 : src
    touch src/main.cpp
    mkdir test      # 8. 테스트   : test
}

Create_CMakeLists_Text
Set_CMake_FolderStructure

cd ../

23-10-27-14-47-24.png


CMakeLits.txt 는 처음에 다음과 같이 실습을 따라했습니다.
다만, 저만의 프로젝트 이름을 설정하기위해 "Phello-cmake"로 이름을 지었구요.

# 최소 CMkae버젼 요구사항 명시
cmake_minimum_required(VERSION 3.0.0...3.27.7)

# 프로젝트 이름 지정
project(Phello-cmake)

# 타겟 프로그램 정의
add_executable(Phello-cmake src/main.cpp)

# 조건문
if(BUILD_TESTING)
    # 메세지 출력
    message('Hello Test')
endif()

cmake -G "Unix Makefiles" -B build-make와 같이
Makefiles를 빌드시스템을 명시하고 빌드할때는 문제가 없이
build-make 파일이 잘 생깁니다.

다만,

cmake -G "Xcode" -B build-xcode 실행할때 다음과 같은

cmake -G "Xcode" -B build-xcode
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:6 (project):
  No CMAKE_C_COMPILER could be found.



CMake Error at CMakeLists.txt:6 (project):
  No CMAKE_CXX_COMPILER could be found.



-- Configuring incomplete, errors occurred!

이 에러를 봤을때, 컴파일러를 명시하면 되겠다 생각하고
다음과 같이 컴파일러를 명시했습니다.

# 최소 CMkae버젼 요구사항 명시
cmake_minimum_required(VERSION 3.0.0...3.27.7)

#
set(CMAKE_C_COMPILER /usr/bin/clang)
set(CMAKE_CXX_COMPILER /usr/bin/clang++)

# 프로젝트 이름 지정
project(Phello-cmake)

# 타겟 프로그램 정의
add_executable(Phello-cmake src/main.cpp)

# 조건문
if(BUILD_TESTING)
    # 메세지 출력
    message('Hello Test')
endif()

그러나 다음과 같이
Check for working C compiler: /usr/bin/clang - broken

The C compiler
"/usr/bin/clang"
is not able to compile a simple test program.

과 같은 이유로 실습진행이 막혔습니다.

cmake -G "Xcode" -B build-xcode
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /usr/bin/clang
-- Check for working C compiler: /usr/bin/clang - broken
CMake Error at /opt/homebrew/Cellar/cmake/3.27.7/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "/usr/bin/clang"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/Volumes/Transcend/개발 자료/Markdown/SelfStudy/Doc/Cpp/CMake/실습/Phello-cmake/build-xcode/CMakeFiles/CMakeScratch/TryCompile-Qo6pMj'

    Run Build Command(s): /usr/bin/xcodebuild -project CMAKE_TRY_COMPILE.xcodeproj build -target cmTC_2733e -parallelizeTargets -configuration Debug -hideShellScriptEnvironment
    Command line invocation:
        /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project CMAKE_TRY_COMPILE.xcodeproj build -target cmTC_2733e -parallelizeTargets -configuration Debug -hideShellScriptEnvironment

    User defaults from command line:
        HideShellScriptEnvironment = YES
        IDEPackageSupportUseBuiltinSCM = YES

    2023-10-27 13:21:07.547 xcodebuild[8028:99890] Writing error result bundle to /var/folders/yc/p3zthqjj70b5_p976b68c9rr0000gn/T/ResultBundle_2023-27-10_13-21-0007.xcresult
    xcodebuild: error: 'CMAKE_TRY_COMPILE.xcodeproj' does not exist.



  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:10 (project)

그래서 두번쨰로 환경변수를 설정했습니다.

/* ~/.zshrc */
:
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++

autoload -Uz compinit && compinit
bindkey '^I' complete-word
zstyle -e ':completion:*' command-path 'reply=( "$PWD/bin" "$path[@]" )'

typeset -U PATH path
:

그럼에도 여전히 빌드가 되지 않고, 마지막으로 Xcode SDK 경로 설정까지 해

/* ~/.zshrc */
export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)

추가를 해봤지만 작동이 되지 않았습니다..
어떻게 해야 해결이 될지 알려주실 수 있을까요? ㅠㅠ

답변 1

답변을 작성해보세요.

1

안녕하세요?

 

예제가 실행되지 않아 어려움을 겪고 계시는군요. 저와 함께 하나씩 풀어가시죠. XCode Command Line Tools가 설치되어 있으시니 사소한 문제로 XCode 빌드 시스템 파일들이 생성되지 않는다고 생각됩니다. 만약 XCode Command Line Tools를 설치하지 않으셨다면 xcode-select --install로 설치하신 뒤 다시 실행해보시길 바라겠습니다.

 

제 개발환경이 HongSangJoon님과 동일한 개발환경이라고 생각됩니다. 제가 실행했을때는 정상적으로 실행이 됩니다.

image

 

맨 처음 문제는 CMake가 컴파일러를 못찾고 있습니다. 이는 CMakeCache.txt가 폴더에 남아있어서 발생할 수 있는 문제이니 폴더를 삭제하고 다시 CMake 명령어를 사용해보셨으면 합니다.

 

컴파일러를 찾지 못하는 문제를 해결하기 위해 Clang 컴파일러 경로를 직접 입력하셨습니다.

Check for working C compiler: /usr/bin/clang - broken

The C compiler
"/usr/bin/clang"
is not able to compile a simple test program.

위 에러 메세지로 유추해 보았을때 컴파일러에 문제가 있어보입니다. CMake는 간단한 코드로 컴파일을 해보고 컴파일이 성공한 경우에 컴파일러를 찾았다고 합니다. 올바른 Clang이 설치되어 있는지 확인 부탁드립니다.

❯ clang --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

제대로 설치되어 있다면 test.c를 만드시고 아래처럼 간단한 코드를 작성하십시요.

#include <stdio.h>

int main() {
    printf("Hello C");
    return 0;
}

그리고 clang test.c를 입력해서 컴파일이 제대로 되는지 확인 부탁드립니다.

 

지금 문제를 근본적으로 해결하기 위해서는 컴파일러의 경로를 강제로 입력하지 말고 CMake가 컴파일러의 경로를 자동으로 찾도록 수정해야합니다. 알려드린 방법을 시도해보시고 안되시면 로그 공유 부탁드려요.

빠른 답변 감사합니다.

답변확인후 여러가지를 또, 세팅을 해봤습니다.

xcode-select --install이 이미 설치 되어있어서

xcode-select --install

> xcode-select: note: Command line tools are already installed. Use "Software Update" in System Settings or the softwareupdate command line interface to install updates

혹시나 하는 생각에

XCode Command Line Tools를 재설치 하는 방법을 선택했습니다.

sudo rm -rf /Library/Developer/CommandLineTools 
xcode-select --install

두번쨰로, 강의를 잘 보지 못한 탓이였는지,
모든 문제가 CMakeCache.txt 있다는 점을 다시 확인해
모든 폴더를 삭제하고 다시 CMakeLists.txt 등등, 폴더 구조를 만들었습니다.

아, 그리고 제가 관과한점이 있는데.
정확히는 vscode내 터미널을 사용중이였습니다. 확인해보니


iterm2의 환경변수 PATH와
Vscode 환경변수 PATH가 다르다는 점을 알고 나서

terminal_integrated_inheritEnv.png

vscode JSON 세팅에 아래 커맨드를 추가함으로

"terminal.integrated.env.osx": {
    "PATH": ""
}

이 두가지 설정을 해줬습니다.

여기까지 하니, 환경변수가 동일해진것을 확인했습니다.

그다음 테스트용 .c파일을 만들고 clang으로 컴파일 해줬더니,
a.out 파일이 잘 나오고, 실행이 잘 되었습니다.

이, 모든것들을 해결하고 나서 다시한번
cmake -G "Xcode" -B build-xcode . 를 실행하니
빌드가 잘 된것을 확인했습니다 ㅠㅠ

정말 감사합니다. 이제 마음놓고 강의를 더 진행할 수 있게되었습니다!

cmake -G "Xcode" -B build-xcode         
CMake Deprecation Warning at CMakeLists.txt:2 (cmake_minimum_required):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- The C compiler identification is AppleClang 15.0.0.15000040
-- The CXX compiler identification is AppleClang 15.0.0.15000040
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (8.9s)
-- Generating done (0.0s)
-- Build files have been written to: /Volumes/Transcend/개발 자료/Markdown/SelfStudy/Doc/Cpp/CMake/mypractice/Phello-cmake/build-xcode

 

근본적으로 분석하시고 해결하셨네요! 이제 어떤 문제도 해결하실 수 있을꺼에요! 사소하다고 생각하시는 질문도 언제든지 환영이니 많은 질문 부탁드려요. 그럼 즐코딩하세요!