Skip to content

Instantly share code, notes, and snippets.

@shimarin
Last active February 22, 2021 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shimarin/99b995cb5fc7970fd7243e5f451f81b4 to your computer and use it in GitHub Desktop.
Save shimarin/99b995cb5fc7970fd7243e5f451f81b4 to your computer and use it in GitHub Desktop.
VS Codeで、複数のC++ソースファイルが置いてあるフォルダで編集中ファイルの実行をできるようにするための tasks.jsonと外部 Makefile
rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
OBJS=$(subst .cpp,.o,$(call rwildcard,,*.cpp))
BINS=$(subst .cpp,,$(call rwildcard,,*.cpp))
HEADERS=$(call rwildcard,,*.h)
CXXFLAGS ?= -std=c++2a
all: $(BINS)
libproj.a: $(OBJS)
ar r $@ $(OBJS)
%: %.cpp libproj.a
g++ $(CXXFLAGS) -o $@ -D__MAIN_MODULE__ $< -L . -lproj
%.o: %.cpp $(HEADERS)
g++ $(CXXFLAGS) -c $< -o $@
clean:
rm -f $(BINS) $(OBJS) libproj.a
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ アクティブなファイルのビルド",
"command": "make",
"args": [
"CXXFLAGS=-std=c++2a -g",
"-j", "-f", "Makefile.vscode",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "デバッガーによって生成されたタスクを外部Makefileを利用するよう改造したもの。"
}
],
"version": "2.0.0"
}
@shimarin
Copy link
Author

編集中のソースファイルは -D__MAIN_MODULE__でコンパイルされるので、各ソースで下記のようにして条件付きで main関数が有効になるようにする。

static int _main(int argc, char* argv[])
{
    std::cout << "Hello, World!!" << std::endl;
}

#ifdef __MAIN_MODULE__
int main(int argc, char* argv[]) { return _main(argc, argv); }
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment