I am just learning Odin but I am trying to include all imports in a single header file as well as variables in a separate file as well, which I include in the imports file, then I only use the import file in the main file so I don’t have to type all the files and variables in one directory.
I can’t figure out how to do this in Odin, I thought packages was supposed to be similar to this? And import does nothing even though I am in the same directory.
EXAMPLE: IN C LANGUAGE
I create a file called main, I write #include “use_files.h”
then I write the main function and the usual printf function and the variable name in between the parentheses.
Then I create another file called use_files.h and write #include <stdio.h> and #include “variables.h”
Then create variables.h and write int num = 2;
so on and so forth. It works in C but not in Odin. Is there anything I can do to make this happen in Odin?
I appreciate any help in advance! Hope y’all have a great day!
Odin’s file naming, package organization, and imports work a little different than C++. Firstly, there are no header files (mostly a C++ thing) in Odin which is one of the reasons Odin is more C-like. All files for a given package are in the same folder with the same package name at the top. There is no need to import anything between files in the same package. All files in the same package know about each other. Do not create a file just for external package imports. Odin requires each file to import any external packages that specific file is using. This may not make sense at first, but give it time, the benefits will become obvious eventually. Also, as Yoda would say, “you must unlearn what you have learned”. Do not try to organize code in separate files like is common in C++. Most of the time, all of your code will live in a single file, or a handful of files at best. All in the same folder, with the same package name at the top. Organizing things in a more complicated way than that is more of an advanced thing to be done for very specific reasons, and even then, I would recommend to not taxonomize code by splitting it up if possible.
Start a new project folder. Name it what ever you like. In that folder, create a file named “main.odin” and copy and paste the following in it. The file does not need to be named “main.odin”, but since this is the file where main will live, the file name reflects that. I personally name the file for my main to have the same name as my package. So in this case, I might have gone with “practice.odin”.
package practice
import "core:fmt"
main :: proc () {
fmt.println("Hellope world!!")
fmt.println(hellope)
}
Create a second file in your project folder and name it “vars.odin”. Place this code in it
package practice
hellope := "Hellope world using variable in vars.odin"
Navigate in a terminal to inside the folder you created for this exercise. Build with:
odin build .
An executable will be created based on the folder name this package is in. Run that executable in the terminal to see output.
Hello xuul, the code snippets worked, thank you. I will follow the links you posted and learn more about organizing the code Odin-wise. It will be tough to unlearn what I’ve learned though.
Package seems simple yet complicated at the same time for me. I feel as though I got the concept, but don’t quite understand how to use it.
It feels like Bill took C and Pascal and interweaved them, which is cool.