include a secondary file in main go file - file

I have a main.go file that I worked on and now I'm trying to organize since it became a little lengthy. I want to create a new file, put some functions in it and then include it in main.go and use those functions. That new file will be in the same directory as main.go. Anybody have any idea how to do this?

As long as the go files are in the same package, you do not need to import anything.
Example:
project/main.go:
package main
import "fmt"
func main() {
fmt.Println(sayHello())
}
project/utils.go:
package main
func sayHello() (string) {
return "hello!"
}
To run: go run main.go utils.go or go run *.go

You don't have to do any including (importing). Just use the same package name in both files.

Old question, but no matter...
You can create go.mod file at the same directory you keep your source files with the help of following command:
go mod init main
This will create mod file for main package.
Each source file should start with package main directive.
After that you can build your project as follows:
go build .
And if you need build and run:
go run .

Related

Cant find project for PackageCompiler

I am trying to create an executable using PackageCompiler.jl. However, whenever I run the command
create_app("src/UnsteadyFlowSolvers.jl","UNSflowCompiled")
I keep getting the error
ERROR: could not find project at "C:\\Users\\Matthew\\OneDrive - Mississippi State University\\Research\\UNSflow\\Ramesh Live\\src\\UnsteadyFlowSolvers.jl"
This is the exact location of the file. For instance:
include("src/UnsteadyFlowSolvers.jl") ; UnsteadyFlowSolvers.julia_main()
works perfectly fine and generates the exact result I would like the executable to return. The Project.toml file is in the current directory if that matters. I have tried an alternate version of the package where the module file is not located in another directory to no avail.
Thanks
create_app(package_dir::String, compiled_app::String; kwargs...)
Compile an app with the source in package_dir to the folder compiled_app.
So the first argument to create_app is the package folder, not the .jl file. I'd suggest a ;cd .. to get outside your project folder, then
create_app("Ramesh Live", "UNSflowCompiled")

How do I create an R package that depends on a shared library with Rcpp?

I have a shared library libbcd.so generated from C source code.
I also have some interface code Rinterface.h, Rinterface.cpp, which looks as follows:
Rinterface.h:
#pragma once
#include <R.h>
#include <Rcpp.h>
extern "C" {
#include "bcd.h"
}
void call_bcd();
Rinterface.cpp
#include "Rinterface.h"
// [[Rcpp::export]]
void call_bcd() {
mm_problem* pr = mm_problem_alloc();
bcd_vars* vars = bcd_vars_alloc();
bcd(pr, vars);
}
Where mm_problem_alloc(), bcd_vars_alloc(), bcd(mm_problem*, bcd_vars*) are all functions from libbcd.so and mm_problem and bcd_vars are user defined types from libbcd.so.
I have created a package with Rcpp.package.skeleton("bcd") put the Rinterface files under src folder with the following Makevars
PKG_CPPFLAGS=-I/usr/local/lib/R/site-library/Rcpp/include/ -I/path/to/bcd/headers/
PKG_LIBS=-L/path/to/bcd/library/file -lbcd
I can create the interface library with R CMD SHLIB Rinterface.cpp
I can also build a package tarball with R CMD build .
If I try to install the package from source with install.packages("/path/to/bcd_1.0.tar.gz", type = "source") after the compilation step I get an error when it tries to load libbcd.so:
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘bcd’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/home/gomfy/R/x86_64-pc-linux-gnu-library/3.6/00LOCK-bcd/00new/bcd/libs/bcd.so':
libbcd.so: cannot open shared object file: No such file or directory
Based on this post it seems to me that a workaround would be to rename the .c source files of libbcd.so to .cpp and then put them in the src folder. Is this correct?
In general, what is a recommended way of leveraging Rcpp to be able to call a third party shared library from R? I've found a related question here but it doesn't go into details of building the package. I've looked at Writing R extensions vignette("Rcpp-introduction"), vignette("Rcpp-package") but I wasn't able to come to a clear conclusion. So, I'd appreciate an expert's help here. Thank you!
In short, you can't.
A system-level shared library will be loaded on demand for you by ld.so (on Linux, similar on other OSs and same idea). So if your package depends on a non-standard or uncommon shared library, tough luck. This can only work if ld.so knows about it, and an R package cannot make it so.
What you can do is bundle the sources of your libbcd with your package and them built as a static library linked to from your package. That will work. As will the simpler approach of maybe just putting the source files of libbcd into the package src/ directory -- and R will take care of the rest.

Module for python3.6.2 (Spyder environment) from c source code

I'm quite new to python that I always use by writing script in spyder and running them in its Ipython console with python3.6.2 .
I'm trying to write a simple module from a "swig_example.c" file following a couple of swig tutorial (http://www.swig.org/tutorial.html, http://www.swig.org/Doc1.3/Python.html#Python_nn6).
My aim is to be able to run a script "main_python.py" which should look like:
import swig_test
print(swig_test.fact(4))
where fact is a function defined in the original c source.
The source file is "swig_example.c":
/* File: swig_example.c */
#include "swig_example.h"
int fact(int n) {
if (n == 0) {
return 1;
}
else {
return n * fact(n-1);
}
}
The header file is as simple as:
/* File: swig_example.h */
int fact(int n);
and the interface one:
/* File: swig_example.i */
%module swig_test
%{
#include "swig_example.h"
%}
%include "swig_example.h"
When in the terminal I run:
swig -python swig_example.i
a "swig_example_wrap.c" and a "swig_test.py" files are created.
How should I proceed to have my "main_python.py" working?
(It now returns a "No module named '_swig_test' error).
I would like to have some script (maybe using distutils?) so that each time I modify the .c source I can easily update the module without changing the "main_python.py" file.
If you have any solution which uses Xcode instead of spyder it would be well accepted.
I think that the question could be useful to many that are new to python (and Mac actually...) and try to use it while not throwing away their previous works...
EDIT:
I -partially- solved the problem. Now the main point remain Spyder.
I create the files ".c", ".h" and ".i" the way I described. Then, following this post (Python.h not found using swig and Anaconda Python) I create, in the same folder, my "setup.py" file:
from distutils.core import setup, Extension
example_module = Extension('_example', sources=['example.c','example.i'])
setup(name='example', ext_modules=[example_module], py_modules= .["example"])
Then, In anaconda navigator I open the terminal of the environment I'm working in, move to the right folder and run:
python setup.py build_ext --inplace
If now I open spyder everything works the desired way. But If I now want to modify my C source, say add a new function, problems arises. I modify the ".c", ".h" and ".i" files annd thenn re-run in the terminal the previous line. The "example.py" file result to e correctly modified (it innncludes the attribute of the new function), but when try to import the module in spyder (import example) changes are nnot registered and an error message "_example has no attribute "new function" is given in the Ipython console unless I restart Spyder itself.
Is there faster way to fix it? (maybe this is the interaction mentioned in the comments... )
Thank you all :-)

XCODE error: new file not built

So first I made a new project by going to File> OS X > Command Line Tool and chose C as the language. This is what I got:
Then I made a new C file called program.c:
Then I pressed run. It didn't build successfully:
However, when I deleted the first default file (main.c) and then pressed run program1.c....it built successfully.
My problem: Why can't I have several files under the same project and run them successfully?
Because you will have the main function declared in both files. I do not know how to tell XCode to build two separate applications..
You can't have 2 main functions
Edit: each c program has to contain only one main function.
Other files of that program can not implement another main function.

Netbeans EXE not working when executed from Terminal - CentOs

I made Netbeans work environment with SASL. The sample codes get build and it also run properly from Netbeans. But when I try to run my exe from Terminal it is not working. The error says as below:
./cppapplication_1: error while loading shared libraries:
libanonymous.so.2: cannot open shared object file: No such file or
directory
I tried setting the PATH using the below Command :
export PATH=/usr/lib64/sasl2/:$PATH
Still I am getting the same error. Do I need anything extra to do?
You need to add the path to libanonymous to the enviroment variable LD_LOAD_LIBRARY.
Update:
To do so:
Locate the library, for example doing: find / -name "libanonymous.so.2" or by locate libanonymous.so.2
Add the path found like so: export LD_LOAD_LIBRARY=$LD_LOAD_LIBRARY:<path to lib>
Update 1:
From your comment to Anon's answer I see that the lib in question is located under /usr/lib64/sasl2/.
So you might like to set LD_LOAD_LIBRAY path like so:
export LD_LOAD_LIBRARY=$LD_LOAD_LIBRARY:/usr/lib64/sasl2/
Update 2
This needs to be done in the same shell that later then executes the program needing the libraries (cppapplication_1).
cd <dir for cppapplication_1>; export LD_LOAD_LIBRARY=$LD_LOAD_LIBRARY:/usr/lib64/sasl2/; ./cppapplication_1
You can also try this.
ldd <name of executable>
You will see dependent libs and their expected paths. See if the lib is present at the path executable is expecting.

Resources