05-13-2019 11:07 AM - edited 05-13-2019 11:11 AM
Is there any workaround to write C code in the block diagram of LabVIEW? I know that I can call an already written C code, however, I wonder if it is somehow possible to write the C code inside LabVIEW block diagram?
I tried the C generator module, but I failed to write and compile any C code. Also, I even failed to generate C code from any LabVIEW code!
Solved! Go to Solution.
05-13-2019 12:09 PM
You can use formula node.
05-13-2019 12:45 PM - edited 05-13-2019 12:48 PM
Your request makes no sense. Can you give us some background information on what you are trying to achieve.
Writing a dll is the correct way in both directions (c dll used in LabVIEW, LabVIEW dll called in c).
05-13-2019 02:26 PM
@muneerkun wrote:
Is there any workaround to write C code in the block diagram of LabVIEW? I know that I can call an already written C code, however, I wonder if it is somehow possible to write the C code inside LabVIEW block diagram?
I tried the C generator module, but I failed to write and compile any C code. Also, I even failed to generate C code from any LabVIEW code!
That's like asking if you can get a copy of "A Midsummer Night's Dream" in Pig Latin.
05-13-2019 02:55 PM
@paul_cardinale wrote:
That's like asking if you can get a copy of "A Midsummer Night's Dream" in Pig Latin.
No, that's actually an easily solvable problem. There are plenty of pig latin translators available online. 😄
05-13-2019 03:13 PM
To give a slightly less sarcastic response...
c is a compiled language, not an interpreted one. The formula node linked uses a "c like" syntax, but is NOT c. In order for c to actually be called from the block diagram, LabVIEW would need to contain an entire c compiler... and then basically repeatedly generate a dll (which you said you don't want to use) each time the vi was recompiled - which LabVIEW does almost constantly in the background as you're working in that vi.
That having been said, LabVIEW does have some options if you're willing to use an interpreted language rather than a compiled one. Primarily the Python node to call python code directly, and the MATLAB script node.
05-13-2019 08:09 PM
If you're willing to wait a small time, and you don't have huge libraries in mind, you can consider writing something that takes your input string, writes it to a file, calls an existing C compiler using the System Exec node, then executes (if desired) the compiled result again using the System Exec node.
Note that there are limitations on your ability to interact with that program during execution, so it might not really solve the problem you want.
As others have already said, it sounds like maybe some more detail on what and why you're trying to do would lead to better suggestions (but I think this will literally do what you want, which might not be the best plan for what you maybe want but didn't say - then again, maybe it really is what you want).
05-14-2019 09:14 AM - edited 05-14-2019 09:19 AM
@zou wrote:
You can use formula node.
Thank you, I tried it previously but it is only limited for mathematical operations, if conditions and repetition loops.
05-14-2019 10:13 AM
Thank you altenbach , paul_cardinale ,BowenM , cbutcher
What I want is to run already written C codes in LabVIEW. I want to use only one software to be an environment for both C codes and graphical codes. Graphical codes are perfect for modeling and simulation. C codes are perfect for programming and coding embedded systems. Combing both of them in one environment is totally awesome!
I used to use MATLAB software to combine different styles of codes and run them. I am trying to use LabVIEW as an alternative to MATLAB. That's because LabVIEW provides more easier and flexible user interfaces (controllers and indicators). I wrote a simple counter program using C code (consists of I/O library, main function and for loop only), I failed to run it in all LabVIEW formula nodes, even the C generator one!
So, only if LabVIEW can deal with C codes, it will be a great alternative. I thought that any software is initially made using C codes and instructions. So, I thought any compiling software can compile C codes in the first place.
Excuse me for my weird wishes and I'm appreciated for your help
05-14-2019 10:40 AM - edited 05-14-2019 10:40 AM
Although I believe that large chunks (someone else could clarify a fraction) of LabVIEW is coded in C++, that doesn't mean it can compile c or c++ code. Imagine a "Hello World" application that takes a minute to write vs the hundreds of thousands of lines (millions, many millions?) For the GCC code base.
If your code is already compiled (e.g. with GCC) you can execute it directly from within LabVIEW using the System Exec node I mentioned. If you want to compile it "in" LabVIEW, you'll need an actual C compiler (like GCC) to do the compiling, but you could call it from LabVIEW.
For more complicated projects, you could call a tool like Make from LabVIEW, which would then call things like GCC. That would require a Makefile (or equivalent file for a different build system) but in principle at least you could write these with LabVIEW's file I/O functions.
I just don't know it's a good idea in reality. For a simple case, it might be a fun tool (e.g. one file applications). For a library or something requiring a build system, probably better to use a dedicated tool in the environment it was designed for.