Add library linking support, update --init

This commit is contained in:
2025-04-16 10:38:30 +10:00
parent f39dd71943
commit 942b3ce836
3 changed files with 47 additions and 5 deletions

View File

@@ -22,8 +22,20 @@ void log(string input) {
cout << "\033[37mInfo: \033[39m" << input << endl;
}
int compile(string compiler, string source, string binary) {
string cmd = compiler + " " + source + " -o " + binary;
int compile(string compiler, string source, string binary, vector<string> libVector, vector<string> incVector, vector<string> libDirVector) {
string libs;
for (int i = 0; i < libVector.size(); i++) {
libs += (" -l" + libVector[i]);
}
string includes;
for (int i = 0; i < incVector.size(); i++) {
libs += (" -I " + incVector[i]);
}
string libDir;
for (int i = 0; i < libDirVector.size(); i++) {
libs += (" -I " + libDirVector[i]);
}
string cmd = compiler + libs + " " + source + " -o " + binary;
log("Compiling with command " + cmd);
return system(cmd.c_str());
}
@@ -99,9 +111,9 @@ int main(int argc, char* argv[]) {
debug("Created source directory");
ofstream srcFile("src/main." + language);
if (lang == "1") {
srcFile << "#include <stdio.h>\nint main() {\nprintf(\"Hello, World!\\n\");\nreturn 0;\n}\n";
srcFile << "#include <stdio.h>\n\nint main() {\n printf(\"Hello, World!\\n\");\n return 0;\n}\n";
} else if (lang == "2") {
srcFile << "#include <iostream>\nusing namespace std;\nint main() {\ncout << \"Hello, World!\" << endl;\nreturn 0;\n}\n";
srcFile << "#include <iostream>\n\nusing namespace std;\n\nint main() {\n cout << \"Hello, World!\" << endl;\n return 0;\n}\n";
}
srcFile.close();
debug("Created source file");
@@ -179,6 +191,9 @@ int main(int argc, char* argv[]) {
string compiler;
string binary;
string source;
vector<string> libraries;
vector<string> includes;
vector<string> libdirs;
string editingTarget;
@@ -187,7 +202,7 @@ int main(int argc, char* argv[]) {
debug("Token is " + parsed[i][j]);
if (j == 0) {
if (parsed[i][j] == "compile") {
compile(compiler, source, binary);
compile(compiler, source, binary, libraries, includes, libdirs);
break;
}
editingTarget = parsed[i][j];
@@ -202,6 +217,15 @@ int main(int argc, char* argv[]) {
} else if (editingTarget == "source") {
debug("Setting source");
source = parsed[i][j];
} else if (editingTarget == "library") {
debug("Adding library");
libraries.push_back(parsed[i][j]);
} else if (editingTarget == "include") {
debug("Adding include");
includes.push_back(parsed[i][j]);
} else if (editingTarget == "libdir") {
debug("Adding library directory");
libdirs.push_back(parsed[i][j]);
}
}
}