cmakeでビルド&実行できたC++コードをSublimeText上からビルドしたいなと思った話。
デフォルトのままだとmake
が実行されないので、設定をちょい変。
お試しのファイル
適当に作る
- cpp, h: printf出力する関数のファイルとメイン
- CMakeLists.txtも最低限必要と思われる記述
-rw-r--r--@ 1 sadoru staff 362 1 10 14:12 CMakeLists.txt
-rw-r--r-- 1 sadoru staff 142 1 10 14:12 func.cpp
-rw-r--r-- 1 sadoru staff 188 1 10 14:12 func.h
-rw-r--r-- 1 sadoru staff 124 1 10 14:12 hello.cpp
func.h
#ifndef FUNC_H
#define FUNC_H
#ifndef __cplusplus
# error ERROR: This file requires C++ compilation (use a .cpp suffix)
#endif
void func1(void);
#endif // FUNC_H
/* EOF */
func.cpp
#include <stdio.h>
#include "func.h"
void func1(void)
{
printf ("%s bbb\n", __PRETTY_FUNCTION__);
return;
}
/* EOF */
hello.cpp
#include <stdio.h>
#include "func.h"
int main(int argc, char *argv[])
{
printf ("aaa\n");
func1();
return 0;
}
CMakeList.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0)
SET(CMAKE_C_FLAGS_RELEASE "-Wall -O2")
SET(CMAKE_C_FLAGS_DEBUG "-g -Wall -O2")
#SET(CMAKE_BUILD_TYPE Release)
SET(CMAKE_BUILD_TYPE Debug)
PROJECT(hello)
ADD_EXECUTABLE(hello
hello.cpp
func.cpp
)
cmakeだけでビルド
$ cmake .
-- The C compiler identification is AppleClang 6.0.0.6000056
-- The CXX compiler identification is AppleClang 6.0.0.6000056
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/sadoru/Desktop/sample
$ ls -l
total 88
-rw-r--r-- 1 sadoru staff 12439 1 10 14:14 CMakeCache.txt
drwxr-xr-x 12 sadoru staff 408 1 10 14:14 CMakeFiles
-rw-r--r--@ 1 sadoru staff 362 1 10 14:12 CMakeLists.txt
-rw-r--r-- 1 sadoru staff 5432 1 10 14:14 Makefile
-rw-r--r-- 1 sadoru staff 1236 1 10 14:14 cmake_install.cmake
-rw-r--r-- 1 sadoru staff 142 1 10 14:12 func.cpp
-rw-r--r-- 1 sadoru staff 188 1 10 14:12 func.h
-rw-r--r-- 1 sadoru staff 124 1 10 14:12 hello.cpp
$ make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/hello.cpp.o
Linking CXX executable hello
[100%] Built target hello
$ ./hello
aaa
void func1() bbb
ok, 問題無し
Sublimetextからビルド
cmd + B
でビルドするとこうなる
Undefined symbols for architecture x86_64:
"func1()", referenced from:
_main in hello-cff8c8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.2s with exit code 1]
SublimeTextの設定
cmd + shift + P
で
Preferences:Browse Packages
-> Finderでパッケージのフォルダ -> C++.sublime-build
デフォルトの記述だとこう。
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
こっちにする。
"cmd": ["make"],
これでできた。