Add library linking support, update --init

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

BIN
bob

Binary file not shown.

View File

@ -75,3 +75,21 @@ compile;
``` ```
The `compile` line just tells Bob that it's time to compile your code. It will run the compiler with all the options. This is helpful as you don't have to type out your long command to compile everything any time you make a change. The `compile` line just tells Bob that it's time to compile your code. It will run the compiler with all the options. This is helpful as you don't have to type out your long command to compile everything any time you make a change.
If you need to use a library, you can add a line like this in your Bobfile:
```
library "(library)";
```
This will look in your system's default directory for libraries (usually `/usr/lib`). Specify a custom directory with
```
libdir "(libdir)";
```
If you need to include a directory, do it like this:
```
include "(include)";
```

View File

@ -22,8 +22,20 @@ void log(string input) {
cout << "\033[37mInfo: \033[39m" << input << endl; cout << "\033[37mInfo: \033[39m" << input << endl;
} }
int compile(string compiler, string source, string binary) { int compile(string compiler, string source, string binary, vector<string> libVector, vector<string> incVector, vector<string> libDirVector) {
string cmd = compiler + " " + source + " -o " + binary; 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); log("Compiling with command " + cmd);
return system(cmd.c_str()); return system(cmd.c_str());
} }
@ -99,9 +111,9 @@ int main(int argc, char* argv[]) {
debug("Created source directory"); debug("Created source directory");
ofstream srcFile("src/main." + language); ofstream srcFile("src/main." + language);
if (lang == "1") { 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") { } 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(); srcFile.close();
debug("Created source file"); debug("Created source file");
@ -179,6 +191,9 @@ int main(int argc, char* argv[]) {
string compiler; string compiler;
string binary; string binary;
string source; string source;
vector<string> libraries;
vector<string> includes;
vector<string> libdirs;
string editingTarget; string editingTarget;
@ -187,7 +202,7 @@ int main(int argc, char* argv[]) {
debug("Token is " + parsed[i][j]); debug("Token is " + parsed[i][j]);
if (j == 0) { if (j == 0) {
if (parsed[i][j] == "compile") { if (parsed[i][j] == "compile") {
compile(compiler, source, binary); compile(compiler, source, binary, libraries, includes, libdirs);
break; break;
} }
editingTarget = parsed[i][j]; editingTarget = parsed[i][j];
@ -202,6 +217,15 @@ int main(int argc, char* argv[]) {
} else if (editingTarget == "source") { } else if (editingTarget == "source") {
debug("Setting source"); debug("Setting source");
source = parsed[i][j]; 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]);
} }
} }
} }