LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing C code in LabVIEW

Solved!
Go to solution

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! 

0 Kudos
Message 1 of 16
(638 Views)

You can use formula node.

image.png

 

 

 

Message 2 of 16
(611 Views)

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).


LabVIEW Champion Do more with less code and in less time
Message 3 of 16
(597 Views)

@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.

"If you weren't supposed to push it, it wouldn't be a button."
Message 4 of 16
(576 Views)

@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. Smiley Very Happy


LabVIEW Champion Do more with less code and in less time
Message 5 of 16
(563 Views)

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.

Message 6 of 16
(552 Views)

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).

Message 7 of 16
(521 Views)

@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.

 

 

0 Kudos
Message 8 of 16
(485 Views)

Thank you 

 

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 Smiley Very Happy and I'm appreciated for your help

0 Kudos
Message 9 of 16
(462 Views)
Solution
Accepted by topic author muneerkun
05-14-2019 08:46 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.

Message 10 of 16
(454 Views)