cpp osx 4 vscode çoklu dosya derleme

Son adım olarak, c projesinde olduğu gibi birden fazla dosya derleme ve farklı dizinlerdeki dosyaları derleme işine bakacağız.

dosyaları şuradakilerden alıyorum.
https://www.go4expert.com/articles/write-multi-files-program-cpp-t29978/

main.cpp

#include <iostream>
#include <string>
using namespace std;
#include "Mathematics.h"
int main() {
 int num1, num2, result;
 Mathematics maths;
 cout <<"Enter the first number:";
 cin>>num1;
 cout<<"Enter the 2nd number:";
 cin>>num2;
 result = maths.add(num1, num2);
 cout <<"\nThe result of adding two numbers is: "<<result<<endl;
 result = maths.subtract(num1, num2);
 cout <<"The result of subtracting two numbers is: "<<result<<endl;
 result = maths.multiply(num1, num2);
 cout <<"The result of multipltying two numbers is: "<<result<<endl;
 result = maths.divide(num1, num2);
 cout <<"The result of dividing two numbers is: "<<result<<endl;
}

mathematics.cpp


#include "Mathematics.h"
int Mathematics::add(int num1, int num2) {
 return Mathematics::result = num1 + num2;
}
int Mathematics::subtract(int num1, int num2) {
 return Mathematics::result = num1 - num2;
}
int Mathematics::multiply(int num1, int num2) {
 return Mathematics::result = num1 * num2;
}
int Mathematics::divide(int num1, int num2) {
 return Mathematics::result = num1 / num2;
}

mathematics.h


#ifndef MATHEMATICS_H
#define MATHEMATICS_H
#include <iostream>
class Mathematics
{
 int result;
 public:
 int add(int num1,int num2);
 int subtract(int num1,int num2);
 int multiply(int num1,int num2);
 int divide(int num1,int num2);
};
#endif

bunları bir dizine koyuyoruz. daha doğrusu main.cpp'yi değiştirip diğerlerini de yanına koyuyoruz.

Artık birden fazla derlenecek dosyamız olduğu için bunu derleyici satırında yazmamız gerekiyor.
Burada "-o" ile "filan.out" satırlarını da ayırdım whitespace ile ilgili bir uyarı verdiği için

"taskName": "compile g++","command": "g++",
"args": [
"-Wall",
// "-g",
"main.cpp",
"mathematics.cpp",
"-o",
"filan.out"
],
"presentation": {
"echo": true,
"reveal": "always"
},
"group": {
"kind": "build",
"isDefault": true
},
"type": "shell"

sorunsuz şekilde derlenip çalışıyor.

bir sonraki adım, header dosyasını farklı bir dizine taşımak.

inc diye bir dizin oluşturup, mathematics.h dosyasını buraya taşıyıp derliyorum, tabii ki hata veriyor çünkü derleyici o dizine bakması gerektiğini bilmiyor.

bir satır ekliyorum. O satır da derleyici seçeneği -I, yani include dizinlerini gösterdiğim komut.
-Iinc diyorum.

"taskName": "compile g++",
"command": "g++",
"args": [
"-Wall",
// "-g",
"-Iinc",
"main.cpp",
"mathematics.cpp",
"-o",
"filan.out"
],
"presentation": {
"echo": true,
"reveal": "always"
},
"group": {
"kind": "build",
"isDefault": true
},
"type": "shell"

şimdi tamamı düzgün şekilde derleniyor.

Eğer derleyici değil de editör "file not found" hatası verirse yapmamız gereken, inc dizinini editörün de include kısmına eklemek



bunun için c_cpp_properties.json dosyasına ekleme yapıyoruz. aşağıdaki son satırdaki gibi.

"name": "Mac",
"includePath": [
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
"/usr/local/include",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/8.1.0/include",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
"/usr/include",
"${workspaceRoot}",
"${workspaceRoot}/inc"

Nasıl yapılacağı ile ilgili bilgi linkte mevcut
https://code.visualstudio.com/docs/languages/cpp



Yorumlar

Bu blogdaki popüler yayınlar

Nasıl yazılır

makefile ile derleme -1

makefile ile derleme -2