0.
MINGW (GCC로 C언어 컴파일) 및 VSCODE 내 C/C++ extension 설치
(파이썬은 Visual Studio를 공식적으로 쓰고 있으니 참고 바랍니다)
Why 'Segmentation fault' occurs in my cpython module code? (PyObject_Print)
I am trying to print PyObject and 'Segmentation fault' occured. I want to use PyObject_Print. How can I solve the problem? int main(){ Py_Initialize(); /////////////////////////////////////...
stackoverflow.com
c_cpp_properties.json
F1 > C/C++ : Edit Configurations
"includePath": [
"${workspaceFolder}/**",
"C:/파이썬경로/Python37/include"
]
"C:/파이썬경로/Python37/include"를 추가합니다
본인 경로/버전에 따라 Python310 등 바꿔주시면 됩니다
이 설정이 없으면,
#include "python.h"
를 vscode가 못찾아 코드 짜기 힘듭니다
tasks.json
shortcut으로 명령어를 자동으로 실행시켜 줍니다
F1 > task입력 > 'Tasks: Configure ~~'
{
"version": "2.0.0",
"tasks": [
{
"label": "build and run",
"type": "shell",
"command": "gcc -Wall -I'C:/파이썬경로/Python37/include' code.c -L'C:/파이썬경로/Python37/libs' -lpython37 -o code.exe ; .code.exe",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
code.c 및 code.exe는 각자 설정해주면 됩니다
-I 및 -L 로 헤더파일 및 링크를 추가합니다
C언어 메모리를 사용하는 비교적 낮은 레밸에서 코딩을 하기 때문에 -Wall 옵션으로 warning 표시해줍니다 (없어도 됨, -w옵션을 주면 모든 warning 표시하지 않음)
gcc command만 하게 되면 .exe만 만들고 실행은 하지 않으므로,
; 뒤에 실행하는 명령어를 추가합니다 (powershell 기준이며, powershell 아니라면 ; 대신 && 시도하면 됩니다)
그러면 빌드한 후 바로 실행합니다
ctrl+shift+B 를 눌리면 바로 terminal에 print가 찍힙니다
----------MSVC가 공식 파이썬 지원 모듈이므로 cl command로 실행해야 정상적으로 작동합니다--------------
[2023-05-02 수정]
tasks.json은 프로그램 실행 시에만 사용,
디버깅을 하려면 launch.json 설정해야함
{
"version": "2.0.0",
"tasks": [
{
"label": "Run",
"type": "shell",
"command": "${workspaceFolder}\\example.exe",
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": ["Build with cl.exe"]
},
{
"label": "Build with cl.exe",
"type": "shell",
"command": "cl.exe",
"args": [
"/EHsc",
"/I",
"C:\\Users\\유저\\AppData\\Local\\Programs\\Python\\Python310\\include",
"/I",
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.33.31629\\include",
"/I",
"C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\ucrt",
"/I",
"C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\shared",
"/I",
"C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\um",
"${workspaceFolder}\\example.c",
"/link",
"/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.19041.0\\ucrt\\x64",
"/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.19041.0\\um\\x64",
"/LIBPATH:C:\\Users\\유저\\AppData\\Local\\Programs\\Python\\Python310\\libs",
"/LIBPATH:C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.33.31629\\lib\\x64",
"/Fe:${workspaceFolder}\\example.exe",
],
"group": "build",
},
]
}
정상적으로 실행하기 위해서 window SDK 가 필요함
dependsOn 옵션을 통해 Build with cl.exe가 먼저 실행되고 그 다음 Run이 실행된다
code.c
#include "python.h"
int main(){
Py_Initialize();
PyObject *a = PyLong_FromLong(1);
PyObject *b = PyLong_FromLong(2);
PyObject *c = PyNumber_Add(a,b);
// PyObject_Print(c, stdout, 0);
PyObject* c_str_obj = PyObject_Str(c);
const char* c_str = PyUnicode_AsUTF8(c_str_obj);
printf("%s\n", c_str);
Py_DECREF(a);
Py_DECREF(b);
Py_DECREF(c);
Py_Finalize();
printf("Program completed successfully\n");
return 0;
}
// 3
// Program completed successfully
Visual Studio를 사용한다면 프린트할때 PyObject_Print(c, stdout, 0); 를 하면 됩니다
'프로젝트' 카테고리의 다른 글
[Linux][Gradle] springboot 빌드 및 실행 (0) | 2023.08.13 |
---|---|
GDB (0) | 2023.05.07 |
C 언어 포인터 (0) | 2023.04.03 |
TestEngine with ID 'junit-jupiter' failed to discover tests (0) | 2023.02.01 |
Java - Static Block / Instance Block (0) | 2023.01.21 |