Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Use C function inside C++ code

Posted on 2005-05-04
10
Medium Priority
?
672 Views
Last Modified: 2008-02-01
I need to use a C function that uses namely
GLUint *var = malloc(3*sizeof(GLUint))

To import a texture into OpenGL... Because no C++ version of the code I use works.

My code is C++ code using objects and I need to make this function available to my driver program.  A single function...

How can I do this?  An answer that works within an hour gets an A and my debt.

I need exact plug in code for a function... doesn't need any content to the function just how to use the C function inside a C++ program.

~Aqua
0
Comment
Question by:Andrew Beers
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13932202
Dont' understand where is the problem. You can use any C function inside your C++ as usual.
You will need to include "gl.h" and "glu.h" and link your project with opengl32.lib glu32.lib if you are workin in Windows.
0
 
LVL 10

Author Comment

by:Andrew Beers
ID: 13933316
*laughs*

Write a C++ program using Classes and cout, then write a function to assign an array a certain size using malloc.

Do this and tell me it works

~Aqua
0
 
LVL 2

Accepted Solution

by:
TheC2 earned 2000 total points
ID: 13933567
I'm not sure I unserstood the problem, but I checked and "malloc" is a part of c++ as well, but you need to include "malloc.h".

#include <malloc.h>

class A
{

};

int main(int argc, char* argv[])
{
      char* c = (char*)malloc(10);

      return 0;
}

Hope this helps...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Julian Hansen
ID: 13933670
I have to agree with jaime_olivares on this one.

There is no issue with calling C functions from C++ code and vice versa. I think you need to look for the problem somewhere else in your code.
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 13934083
To link a C library into a C++ codebase, use the extern "C" directive.  Like this:

extern "C"
{
#include "gl.h"
#include "glu.h"
}
...


The 'extern "C" ' tells the compiler NOT to user name mangling when refering to the functions in OpenGL.  You don't have to do this for <malloc.h> because the standard library already does this internally.
0
 
LVL 16

Expert Comment

by:nonubik
ID: 13934162
>Write a C++ program using Classes and cout, then write a function to assign an array a certain size using malloc.
>
>Do this and tell me it works

Well, it works.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13934925
BTW, thanks Julian for explicity supporting me. That's why this is an Expert Forum, the best on the Internet, as far as I know.
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 13935009
No problem - I have had other experts jump in on my behalf - just passing it on and trying to keep in the spirit of things.
0
 
LVL 10

Author Comment

by:Andrew Beers
ID: 13938230

Malloc in C is written as such:

GLUint *a = malloc(numSlots * sizeOf(GLUint));

and the answer I got that I needed is because of something I found out today... a day late and a dollar short also an answer given is you must do this:

GLUint *a = (GLUint*) malloc(numSlots* sizeOf(GLUint));

Considering that is all I asked for and writing the c style exactly DOES give you an error. So you experts tell me this..

Is this C++ version:
   GLUint *a = (GLUint*) malloc(numSlots* sizeOf(GLUint));
Identical to this C version:
   GLUint *a = malloc(numSlots * sizeOf(GLUint));

NO, because malloc returns a void object that has to be static casted to the object type to which you are passing...

So to answer and quelch the argument here, there WAS a problem, I was lacking the GLUint *a = (GLUint*)...

~Aqua

PS:  I appreciate the reply TheC2... a little later than what I needed but at least you gave me the code I asked for.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Screencast - Getting to Know the Pipeline

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question