Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I have successfully designed a code library to drive stepper motor in different control mode,
but i have a problem, what if in further projects i need to control more that one stepper motor.
Is there a method to convert this library to object like class in c# or c++. but i am using ANSI c

Thanks in advance
Posted

You could convert it to a C++ class, but if you need to use it from C, C++ and C# it is best to leave it as C. Accessing a C library from C++ is just the same as from ANSI C. Accessing from C# requires a little extra work[^], but is not too difficult.
 
Share this answer
 
Comments
z3ngew 5-Aug-13 11:27am    
I want to use it from c, but c is not OOP so is there a way to make it look like OOP
oop* object oriented programming
I want to treat this library as an object in C

Thanks,
z3ngew
Richard MacCutchan 5-Aug-13 11:28am    
No, C is a procedural language. And "making it look like OOP" will not provide any significant benefits.
z3ngew 5-Aug-13 11:30am    
ok, but how can i use this library as object in ANSI C, if that make any sense

Thanks,
z3ngew
Richard MacCutchan 5-Aug-13 11:43am    
You can't: C is a procedural language and has no concept of objects. If you want this as an object oriented library then you need to re-write it in C++. But, you would then need to provide a C-based interface for your ANSI C projects. As I said before, this is a lot of work for no significant benefit.
z3ngew 5-Aug-13 11:52am    
thanks for your effort :)
c code can you wrap in classes for c++. -> static functions.

The only way in c#: you can try to build a c-dll and call it from c#.

Good luck :-)
 
Share this answer
 
If you are controlling more than one motor, then converting your code to C++ will offer little benefit. You should be able to do this in C without much change.

Here are a few things for checklist:
- Make sure that your data is not stored in global and/or static variables/structures.
- Make all of your APIs pass around a context variable identifying which motor (etc.) is being controlled.

You don't need C++ or C# to do this. These languages will push you a little harder to do the right thing but you can do it all in C...
 
Share this answer
 
Comments
z3ngew 5-Aug-13 14:31pm    
I agree with you.
I figured out an idea, and am about to make it happen.

Thanks,
z3ngew

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900