I don't understand how to work with F# header files.
I have two test files:
Foo.fs:
module Foo
let add a b = a + b
Program.fs:
open Foo
printfn "%d" (add 8 2)
In the file Program.fs, Visual Studio tells me:
Files located in libraries or applications containing multiple files
must start with a namespace or module declaration.
Only the last source file of an
application can omit such a statement.
However, I did the right thing: start my Foo.fs file with a module declaration. If I declare a namespace or module to Program.fs, the error persists. So I don't have access to the add function.
How do I import this file?
Related
target_link_libraries(${PROJECT_NAME}
serializer
iothub_client
iothub_client_mqtt_transport
umqtt
aziotsharedutil
ssl
crypto
curl
ssl..... utils)
Hello there , I am working on a project where I have a large set of libraries, and have use it like it (shown above). but instead of hardcoding these libraries explicitly,i want write it in a one line.
now I want to write it in a one line for linking, so specifically, what I have tried in the root level I have created a file called "library.lst" and in this .lst file I am giving the path of that library
build/src/con/shared/virtual/serializer/serializer.a"
build/src/con/shared/virtual/iothub_client/iothub_client.a"
build/src/con/shared/virtual/umqtt/ umqtt.a"
build/src/con/shared/virtual/utils/utlis.a
## write for every library
I am placing this .lst file in the folder called "filelist", e.g filelist/Library.lst where it will take all the libraries and will link.
so what I wrote this In each camkelists.txt where this library used by writing
file(STRINGS ${filelist} library) ,
target_link_libraries(${PROJECT NAME} ${library}),
but when I am running this script I an getting the errors like
"error:- "file strings requires and filename and output variable",
and I also have creates build.sh file where I am giving the path of it
"-D"filelist=%FILELISTSPATH%\library.lst"
" so could you please help me here and also need some explanation on it to understand it in a better way.Thanks in advance.
I have a directory and inside of the directory are subdirectories and a python file trying to run other files called Main.py. In each of the subdirectories there will be one file called runner.py with its only function I need to execute is “func”. If I want to use importlib.importmodule how would I go about doing that. As of right now I’m iterating over the directory and using:
filepath = sys.argv\[1\] + "/runner.py"
student_module = importlib.import_module("func", filepath)
But I’m afraid I don’t know how to use it properly as I’m new to python.
In case my words weren’t clear the directory architecture is as follows
Directory
->Main.py
->Subdirectory(1)
->runner.py
->Subdirectory(2)
->runner.py
.
.
.
->Subdirectory(n)
->runner.py
I had tried using the snippet of code above and I was expecting to be able to run the func from the different runner.py files. Unfortunately I’m getting a:
ModuleNotFoundError: No module named ‘func’
This question already has answers here:
How can I include a module from another file from the same project?
(6 answers)
Closed 3 years ago.
My directory structure:
src
main.rs
image.rs
decoders.rs
When I try to import my decoders module in image.rs I get this:
error[E0583]: File not found for module `decoders`
decoders.rs:
pub mod Decoders {}
image.rs:
mod decoders
use decoders::Decoders
pub mod Image {}
Note: I am using a module that wraps the entire file on purpose that's I can put attributes on entire files. This is why it's not a duplicate of How to include module from another file from the same project?
The weird thing is, is that this syntax works perfectly fine when I try to import Image in main.rs:
mod image;
use image::Image;
What's happening is that when you try to import decoders::Decoders in image.rs, you need to go through the next level up, because using this:
mod decoders
use decoders::Decoders
Means that decoders will now be "owned" or under image, which means that the compiler will search in the image subdirectory for decoders.rs. So, to fix this, you can either change your file structure to this:
src/
main.rs
image.rs ** or image/mod.rs
image/
decoder.rs
Or, use this in main.rs:
mod decoders;
mod image;
and this in image.rs:
use super::decoders::Decoders;
//Or alternatively
use crate::decoders::Decoders;
Also, to fix your nested-mod problem, do the following in decoders.rs:
//Your code, no `mod Decoders`
and the following where you have your mod decoders statement:
#[your_attribs]
mod decoders;
The rust compiler resolves modules differently depending on where they're defined.
When you use the mod keyword to declare an external module from the crate entry point (typically main.rs or lib.rs) or from a module root (mod.rs), the compiler will search for files adjacent to the declaring file. This is why it works properly when using mod image.rs in your main.rs file.
In other cases, the compiler will search for files in the folder with the same name as the declaring file. In your case, this means that your mod decoders; line in image.rs results in the compiler searching for the module in the image subfolder - specifically checking image/decoders.rs and image/decoders/mod.rs.
To fix this, you can either move decoders.rs to image/decoders.rs if you want to keep decoders as a submodule of image, or alternatively place mod decoders; in main.rs and leave the file where it is.
In some Rust projects I've seen (i.e pczarn/rustboot), I've seen mod.rs files in directories for whatever reason. I've not been able to find documentation about this, and I've seen it in many other Rust projects.
What is the purpose of a mod.rs file, and when should I use it?
Imagine the following directory structure:
code/
`- main.rs
- something/
`- mod.rs
If in main.rs you do mod something;, then it'll look in the something/mod.rs file to use as the contents of the module declaration for something.
The alternative to this is to have a something.rs file in the code/ directory.
So to recap, when you write an empty module declaration such as mod something;, it looks either in:
a file called something.rs in the same directory
a file called mod.rs in a folder called something in the same directory
It then uses the contents of either of those files to use as the contents of the module declaration.
Modules are important to understand, but I find most documentations often leave you scratching your head on that matter.
Coming from Python or Javascript?
Roughly, mod.rs is kind of like __init__.py in python or index.js in javascript.
But only kind of. This is a bit more complicated in Rust.
Rust is different
Folders are not immediately ready to use as modules in Rust.
You have to add a file named mod.rs in a folder to expose a new module named like that folder.
The code in mod.rs is the content of that module.
All other files in the folder may in turn be exposed as submodules (more on that below).
Wait, there is another way
You may also use a file at the same level as a folder and named after that folder (<folder_name>.rs).
This is the preferred way since rustc 1.30. (Credits to MarkusToman in the comments)
From the Rust reference:
Note: Previous to rustc 1.30, using mod.rs files was the way to load
a module with nested children. It is encouraged to use the new
naming convention as it is more consistent, and avoids having many
files named mod.rs within a project.
Complete example
src
utils
bar.rs
foo.rs
main.rs
At this point, the compiler doesn't know about src/utils/foo.rs and src/utils/bar.rs.
First, you must expose src/utils/. As seen above, you have 2 options:
add the file: src/utils/mod.rs
add the file src/utils.rs (named exactly like the folder, without the extension)
Now, relative to the src folder (aka the crate level), a module named utils is available.
Second, you must expose the files src/utils/foo.rs and src/utils/bar.rs.
To do that, the utils module must declare 2 new submodules named after these files.
So the content of src/utils/mod.rs (or src/utils.rs) should be:
pub mod bar;
pub mod foo;
Now whatever is public in those 2 files is available in other modules! 🎉
And you may write the following in src/main.rs:
mod utils;
use utils::{foo, bar};
Resulting file structure
Option 1 • mod.rs (the old way):
src
utils
bar.rs
foo.rs
mod.rs
main.rs
Option 2 • <folder_name>.rs (the preferred way):
src
utils
bar.rs
foo.rs
utils.rs
main.rs
More advanced details on how modules work
This remains a surface explanation, your next destination is the official documentation 🧑🎓
There is a third way to declare modules (core language):
mod utils {
// module code goes here. That's right, inside of the file.
}
But it is also possible to just write mod utils;.
In that case, as seen above, Rust knows to search for either of src/utils.rs or src/utils/mod.rs.
See, when you try to use a module in a file (in src/main.rs for example),
you may reference it in the following ways:
from inside: src/main.rs
mod module { ... }
from nested modules inside: src/main.rs
mod module { pub mod sub_module { ... } }
from sybling files: src/*.rs
from mod.rs files in sybling folders: src/*/mod.rs
(and infinite reccursive combinations of the above)
A file or a folder containing mod.rs does not become a module.
Rather, the Rust language lets you organize modules (a language feature) with a file hierarchy.
What makes it really interesting is that you are free to mix all approaches together.
For example, you may think you can't directly reference src/utils/foo.rs from main.rs.
But you can:
// src/main.rs
mod utils {
pub mod foo;
}
Important notes:
modules declared in files will always take precedence (because you in fact never need to search the file hierarchy)
you can't use the other 2 approaches to reference the same module
For example, having both src/utils.rs and src/utils/mod.rs will raise the following error at compile time:
error[E0761]: file for module `utils` found at both "src/utils.rs" and "src/utils/mod.rs"
--> src/main.rs:1:1
|
1 | mod utils;
| ^^^^^^^^^^
|
= help: delete or rename one of them to remove the ambiguity
Let's wrap up. Modules are exposed to the compiler:
from top to bottom
by reference only
(That's why you don't have intellisense until your modules are "imported")
starting from an entry point
(which is src/main.rs or src/lib.rs by default.
But it may be anything that you configure in Cargo.toml.
This has little to do with this question however)
With our previous example we get in order:
src/main.rs -> crate
Because the crate module contains mod utils; we next get:
src/utils.rs OR src/utils/mod.rs -> crate::utils
Because the utils module contains mod foo; we next get:
src/utils/foo.rs -> crate::utils::foo
Each rs file, except lib.rs and main.rs, which always match the crate module, gets its own module.
There is only one way to declare a module:
/* pub */ mod sub_module1;
A module cannot be declare outside the root/crate module tree (i.e., going up the module tree, a submodule must always have a parent that is declared directly in lib.rs or main.rs, so the first program submodule must always be declared there — a tree data structure if it isn't already obvious enough).
There are 2 ways to nest a module inside the module where it is declared:
in <module_where_it_is_declared>/<module_name.rs>
in <module_where_it_is_declared>/module_name/mod.rs
If module_where_it_is_declared is the crate module, then this corresponding subfolder is not needed (disappears from the scheme above).
Here is an example, valid for both lib and binary crates:
src
|---lib.rs ( contains: pub mod b2; )
|---b2.rs ( contains: pub mod bb2; )
|---b2
| |---bb2.rs
. .
Alternatively:
src
|---lib.rs ( contains: pub mod b2; )
|---b2
| |---mod.rs ( contains: pub mod bb2; )
| |---bb2.rs
. .
You can see that you can mix and match (b2 uses mod.rs way, bb2 uses the "file"-way).
Here's a way to only use the file pattern that is also valid:
src
|---lib.rs ( contains: pub mod b2; )
|---b2.rs ( contains: pub mod bb2; )
|---b2
| |---bb2.rs (contains: pub mod bbb2; )
| |---bbb2.rs (contains: pub mod bbbb2; )
| |---bbb2
| | |---bbbb2.rs
. . .
I guess it depends on you how you want to nest modules.
I like the mod.rs syntax for modules that just export other submodules and don't have any other (or very little) code in them, although you can put whatever you want in mod.rs.
I use mod.rs similar to the barrel pattern from JS/TS world, to rollup several submodules into a single parent module.
Also don't forget modules can be defined (not only declared) inline by adding a scope block:
pub mod my_submodule {
// ...
}
I'm wondering what decides whether you're allowed to use <Header.h> or "Header.h" when you're importing files in Objective-C. So far my observation has been that you use the quote marks "" for files in your project that you've got the implementation source to, and angle brackets <> when you're referencing a library or framework.
But how exactly does that work? What would I have to do to get my own classes to use the brackets? Right now Xcode will not allow me to do that for my own headers.
Also, by looking in some frameworks headers, I see that the headers reference each other with <frameworkname/file.h>. How does that work? It looks a lot like packages in Java, but as far as I know, there is no such thing as a package in Objective-C.
Objective-C has this in common with C/C++; the quoted form is for "local" includes of files (you need to specify the relative path from the current file, e.g. #include "headers/my_header.h"), while the angle-bracket form is for "global" includes -- those found somewhere on the include path passed to the compiler (e.g. #include <math.h>).
So to have your own headers use < > not " " you need to pass either the relative or the absolute path for your header directory to the compiler. See "How to add a global include path for Xcode" for info on how to do that in Xcode.
See this MSDN page for more info.
In C, the convention is that header files in <> bracket are searched in 'system' directories and "" in user or local directories.
The definition of system and local is a bit vague, I guess. I believe it looks in system directories in include path or in CPPFLAGS for <header.h>, and local directory or directory specified with -I to compiler are searched for "header.h" files.
I assume it works similarly for Objective-C.
To import your own classes using "< >" you have to put the header files (*.h) in the lib folder of compiler or set a SYSTEM VARIABLES ponting to your lib folder.
#import <> vs ""
<Name.h> - Angle brackets tells to preprocessor to search in a special pre-designated system's directories. For example you import systems headers like <UIKit/UIKit.h> or added frameworks
"Name.h" - Quotation marks tells to preprocessor to search in a current directory. If a header was not found the preprocessor try to use <Name.h>. Usually you should use it with your project's files
Just stumbled upon the same problem, there are 2 types of search paths is Xcode:
User Header Search Paths
Header Search Paths
If you add your own include folders into Header Search Paths, you can use angled brackets without any problem.
Or set Always Search User Path to YES so you can use angle brackets.
With angle brackets e.g. <Foundation/Foundation.h> you import system files.
You use double quotes "Person.h" to import local files (files that you created) and to tell the compiler where to look for them.
If this is an Xcode project and you want to include it in a framework, have the header file you want to included open. Then, open Xcode's rightmost tab and under "Target Membership", click on the framework you want your file to available from.
e.g. If your framework is AlphaTools and your header, AceHeader, then you'll select AlphaTools on Target Membership so you can access < AlphaTools/AceHeader.h
WHAT IS HEADER FILE ?
Header files contain definitions of functions and variables which can be incorporated into any C program by using the pre-processor #include statement. Standard header files are provided with each compiler, and cover a range of areas, string handling, mathematical, data conversion, printing and reading of variables.
Ex- #include it contain the information about input like scanf(),and out put like printf() function and etc in a compiler.
INCLUDE
1) #INCLUDE:-
It is a pre-processor that process before process of main function.
The main work of pre-processor is to initialize the environment of program i.e that is the program with the header file.
2).h:-
(Header file) A header file is a file with extension .h which contains C function declarations and macro definitions and to be shared between several source files.
Q) There are two types of header files: the files that the programmer writes and the files that come with your compiler ?
A)In a angular brackets
Angular-bracket form is for "global" includes -- those found somewhere on the include path passed to the compiler (e.g. #include)
It is used for using of library function which is all ready define in compiler.
In C the convention is that header files in <> bracket are searched in 'system' directories
B) Quote marks:- “header.h”
quoted form is for "local" includes of files (you need to specify the relative path from the current file, e.g. #include "headers/my_header.h")
In C the convention is that header files in " " are searched in user or local directories.
In it one file to be included in another .(FILE INCLUSION).
It can be used in two cases:
Case 1: If we have a very large program, the code is best divided int several different files,each containing a set of related functions.
Case 2: There are some functions and micros definitions that we need at most in all programs that we write.
Ex