How can I modify a parameter inside a CMake Macro?
I'm using CMake version 2.6 and I can't update the variable!
Here's a basic example:
# macro definition
MACRO(MYTEST RETVAL)
message("input RETVAL=${RETVAL}")
SET(RETVAL "new return value")
message("after update RETVAL=${RETVAL}")
ENDMACRO(MYTEST)
# call macro with parameter '_test' set to 'init'
SET(_test "init")
MYTEST("${_test}")
message("after macro call:${_test}")
This prints:
input RETVAL=init
after update RETVAL=init
after macro call:init
The variable _test is never modified. What can I do to make it work?
Thanks
With CMake macros, you need to very carefully distinguish between macro parameter name, variable name, variable value etc. It's not quite clear from your CMake code what you want to achieve, but I assume you want to set _test to the string new return value using the macro.
This is the code to accomplish that:
macro(MYTEST RETVAL)
message("input variable name: RETVAL=${RETVAL}")
message("input variable value: ${${RETVAL}}")
set(${RETVAL} "new return value")
message("variable name after update: RETVAL=${RETVAL}")
message("variable value after update: ${${RETVAL}}")
endmacro()
set(_test "init")
MYTEST(_test)
message("after macro call: ${_test}")
Points to note:
You want the macro to modify the variable which was passed in. That variable's name is stored in the macro parameter RETVAL. So you need to set(${RETVAL} ...) to set the varibale. Your code was creating a variable named RETVAL.
You need to pass the name of the variable to change to the macro. So you must pass _test, and not ${_test}. Your code was calling the macro with the text init.
Related
My C++ functions for postgres use a parameter (the path to the config) for static singleton Config class, now it is set from Cmake via definitions. I want to make the path specified like an argv parameter in the main function. However, my functions are just a shared (.so) library. Is there any way to achieve such functionality ?
Now I use this vartiant:
add_compile_definitions(CONFIG_PATH="/some/path/to/config.ini)
Also I have sql script for creating postgres functions:
CREATE OR REPLACE FUNCTION some() RETURNS text
AS 'postgres_Clibrary.so', 'someFunction'
LANGUAGE C;
And load functions as:
psql -f create_functions.sql
And I want to change this to something like:
psql -f create_functions.sql -DPATH_TO_CONFIG="/some/path/to/config.ini"
Is there any variant to get similar functionality as I want ?
How to do it without recompilation ?
I think you could use a default value for a parameter here.
Define your function so that it has an additional parameter config_path. That parameter has to be the last one. Then you can define the function as
CREATE FUNCTION some(
first_param text,
config_path text DEFAULT '/some/path/to/config.ini'
) RETURNS text
AS 'postgres_Clibrary.so', 'someFunction'
LANGUAGE C;
Now you can simply call the function with
SELECT some('first_arg');
and the default value will be substituted for the second argument.
I have a macro A that formats some text
<#macro A text>...${text}...</#macro>
and another macro that has a parameter accepting text
<#macro B x>Another ${x} text</#macro>
I'd like to call B with the x paramter to be some text formatted by A, s.th. like
<#B x="<#A text='abc'/>" /> returns Another <#A text='abc'/>
Is this possible somehow?
I tried the ?interpret as suggested here by ddekany -
<#B x="<#A text='abc'/>"?interpret /> but this fails with the error:
Expecting a string, date or number here, Expression .... is
instead a freemarker.core.Interpret$TemplateProcessorModel
It seems that a macro call in FreeMarker is something different than a function call in other languages.
Macro calls aren't expressions, and hence can't be used inside expression (like a parameter value). Macros are called for their side effects, which is typically printing to the output, and have no return value. Functions (see #function) are called for their return values, and so function calls are expressions. So maybe you need functions, not a macros in this case.
But if you absolutely have to use the output of a macro call in an expression (or of any arbitrary template fragment), then you have to capture the output via <#assign someVar>...</#assign> or <#local someVar>...</#local>. (Beware with #escape. If you re-print the captured output with ${...}, it will be escaped again, so you will need #noescape.)
I found a workaround using assign:
<#assign a><#A text="abc"/></#assign>
<#B text=a/>
Anyway, it would be interesting to know if this is possible somehow.
I have a problem about global variables on Smartface. I create a dataset and I gave a criteria with a value which name is param1. Although I define a variable which name is param1 in Global file, even the code runs correctly I get an error like;
Can't find variable:
param1
*undefined
*1
*global code
As I said, my code runs correctly, but why I always get this error?
I have got such an error like that. Probably, you defined that variable into the one of the functions that are in the Global file. You should directly define global variables into the Global file.
function Global_Events_OnStart(e) {
...
}
--> For example you should define your global variables here, so outside of all the functions.
function Global_Events_OnError(e) {
...
}
I'd like to use the data that are loaded to my workspace in a Matlab function. This is the beginning of my function.
function [totalProfit] = compute(p,exit)
%% Declaration of variables
entry=0;
T = length(data);
.
.
.
end
I'm getting an error:
Undefined function or variable 'data'.
Where is the error?
The variable data was probably defined outside of the function, so it is out of scope.
Pass data as a parameter to compute and then it will be available inside the function.
You can use evalin to work with variables from another workspace. In your example this could be
T = evalin('caller','length(data)')
But please note that in most cases you get cleaner code if you define the variable as input argument for the function. So for your case this would be
function [totalProfit] = compute(p,exit,data)
T = length(data) ;
end
Ran is correct, but I wanted to mention something else. In general, only variables that are passed as arguments to a function are able to be used inside that function, so if you want to use your existing variables inside the function, pass them as input arguments.
It is possible to create global variables which allow you to use them inside functions without passing them as arguments, but it's usually not the best way of writing code. The times where I have used global variables are where I am calling multiple functions from a single script, and I have some constants that will be used by all the functions (for example gravity is a common one). An alternative to global variables is to use a struct, with the variables you want to pass to the function in it, so you only need one extra input argument, but you still have to be a bit careful.
Inside an install4j project, is it possible to refer to a compiler variable within the value of another compiler variable? For example, if I've defined the variable "revision" earlier in the list of variables, is it possible to have another variable named "outputDir" with the following value:
somedir/${revision}/otherdir
Yes, that's possible, you just set the value of outputDir to
somedir/${compiler:revision}/otherdir