Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Using U++ with existing code
Using U++ with existing code [message #24917] Thu, 04 February 2010 14:47 Go to next message
TRNG98 is currently offline  TRNG98
Messages: 5
Registered: January 2010
Location: Lund
Promising Member
I have an existing C program that do all the work I need.
Can I connect this with an U++ application, best would compile them together? I need very little integration, some static global variables would do. Can absolutely not translate the C program
to U++.

//
Best regards
Bo
Re: Using U++ with existing code [message #24920 is a reply to message #24917] Thu, 04 February 2010 15:35 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
Well U++ is normal C++ so the short answer is yes. Long answer is that there are a few steps that are necessary.

First step would be to compile your C program the way you do now, but replace the C compiler with C++ compiler. You might get a few easy to fix compilation errors, unless your program is C99. After you program compiles, create a package you should be done.

A few more extra steps that could be necessary depending on your sources are adjusting your include paths and deactivating BLITZ for the package that contains your former C sources, but in most cases you won't have to do this.
Re: Using U++ with existing code [message #24921 is a reply to message #24917] Thu, 04 February 2010 15:46 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

TRNG98 wrote on Thu, 04 February 2010 14:47

I have an existing C program that do all the work I need.
Can I connect this with an U++ application, best would compile them together? I need very little integration, some static global variables would do. Can absolutely not translate the C program
to U++.

//
Best regards
Bo


Hi Bo!
Since C is a subset of C++ and U++ is still just C++, there is technically no problem in using them together. Also TheIDE is aware of the difference between .c and .cpp files, so if you just add your existing files into the project, they should be compiled and linked correctly. A little work will be needed to bind the two parts (U++ and C) correctly, but that depends a lot on what do you want to do.

Typical case I can imagine would be writing U++ GUI for C console program. In that case, you would just write the GUI part, calling existing functions from the original sources (including necessary headers of course). Commenting out the original main() or renaming it is probably the only big change you would have to do in original sources in this case. This is just an example, if you supply more info, we can probably give you better hints Wink

If you want to see a practical example, you can have a look into package uppsrc/plugin/ndisasm. That is package that includes C sources of libndisasm to provide disassembler used in TheIDE. It's probably not the best example (it won't compile as standalone app anymore, not sure why Confused ), but it should give you an idea about mixing U++ and C. Actually, most of the packages in uppsrc/plugin are using existing C code, so just have a look in that direction Wink

Best regards,
Honza
Re: Using U++ with existing code [message #24922 is a reply to message #24921] Thu, 04 February 2010 16:12 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3357
Registered: August 2008
Senior Veteran
Hello TRNG98

And additionally to the last comments, do not forget to add a:
extern "C" {
      Declarations of C functions called from C++
}


in the .cpp files where functions in .c are called.


Best regards
IƱaki
Re: Using U++ with existing code [message #24924 is a reply to message #24917] Thu, 04 February 2010 20:37 Go to previous messageGo to next message
TRNG98 is currently offline  TRNG98
Messages: 5
Registered: January 2010
Location: Lund
Promising Member
Thank you for your help -- I'll hack around a while --
The C is multi-threaded and run some API functions (not Win API:s).
I don't want to rewrite it. Surprised

// Bo
Re: Using U++ with existing code [message #24937 is a reply to message #24924] Fri, 05 February 2010 10:17 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
TRNG98 wrote on Thu, 04 February 2010 14:37

Thank you for your help -- I'll hack around a while --
The C is multi-threaded and run some API functions (not Win API:s).
I don't want to rewrite it. Surprised

// Bo



Multithreaded: There is one thing that you should know: U++ uses own memory allocator that need some support in threads - basically, as allocator keeps per-thread cache, this cache has to be released when thread finishes.

For you, in practice, this should not be a problem, as long as you do not use new/delete in threads of your C application. But new/delete is (likely to be) used if actually call any U++ code from any non-main thread!

Simple way how to fix this is to specify "USEMALLOC" flag - that kicks out our memory allocator and uses plain malloc/free from the C library (but performance of U++ drops - IMO not an issue if you app depends on malloc/free anyway).

Best solution is to use U++ Thread class for threading. Good solution is to release the per-thread cache at the end of your threads (by calling MemoryFreeThread()Wink

Mirek
Previous Topic: header file problem
Next Topic: Build error
Goto Forum:
  


Current Time: Thu Apr 25 02:50:09 CEST 2024

Total time taken to generate the page: 0.02887 seconds