Calling an external function in C

What's written in the code to link two files?

Page 1 of 1

7 Replies - 4296 Views - Last Post: 24 March 2010 - 07:45 AM Rate Topic: -----

#1 Guest_Cloudyeyes*


Reputation:

Calling an external function in C

Posted 24 March 2010 - 02:41 AM

I've never called an external function before, using 2 files to compile into one .exe. I want to call one external function in the main source code, then have the second source be used as an external function in the main source.

My example... I want to use this function in my main source:
void CalculateTaxesAndNetPay(float *fedtax, float gross, float deferred,
                float *statetax, float *ssitax, float *netpay);


...and I want this whole thing to be my function from the second source:
void CalculateTaxesAndNetPay(float *fedtax, float gross, float deferred,
                float *statetax, float *ssitax, float *netpay)
        {
                
                *fedtax = 15 * ((gross - deferred)/100); 
                *statetax = 7 * ((*fedtax)/100);
                *ssitax = 7.75*((gross - deferred)/100);
                *netpay = gross - *fedtax - *statetax - *ssitax - deferred;
        }



Do I need to use an extern command? Any help is appreciated. :)

Is This A Good Question/Topic? 0

Replies To: Calling an external function in C

#2 Guest_Cloudyeyes*


Reputation:

Re: Calling an external function in C

Posted 24 March 2010 - 03:23 AM

Ok, I did this...(added extern "C"). By the way, I'm using Visual Studio 2008 to compile..
extern "C" void CalculateTaxesAndNetPay(float *fedtax, float gross, float deferred,
                float *statetax, float *ssitax, float *netpay);


and then renamed the CalculateTaxesAndNetPay.cpp to CalculateTaxesAndNetPay.obj

Now it's telling me "fatal error LNK1107: invalid or corrupt file: cannot read at 0x1AC"
Was This Post Helpful? 0

#3 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Calling an external function in C

Posted 24 March 2010 - 03:44 AM

A *.cpp file is Source Code, and must be TRANSLATED by a compiler into object code, a *.obj file.
You can't just rename a cpp file into an obj file!
Normal procedure is to add the 2nd sourcefile to your project (project/add file) then locate your c/cpp file.
Then the framework (Visual studio) will instruct the compiler to generate an obj file from it, and also tell the linker to join it to your executable. The linker is now trying to add your obj file, but it isn't a proper obj file, so the linker complains!

This post has been edited by Ferencn: 24 March 2010 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Cloudyeyes*


Reputation:

Re: Calling an external function in C

Posted 24 March 2010 - 03:57 AM

Ok, I renamed the .obj file back to .cpp and tried recompiling. I notice it does automatically create a .obj file, like you said, so that's good. However, now it's giving me some very strange error. I'm using extern "C" CalculateTaxesAndNetPay(...etc)

"error LNK2019: unresolved external symbol _CalculateTaxesAndNetPay referenced in function_main"

"error LNK2019: unresolved external symbol _WinMain@16 referenced in _tmainCRTStartup"


"fatal error LNK1120: 2 unresolved externals"
Was This Post Helpful? 0

#5 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Calling an external function in C

Posted 24 March 2010 - 04:07 AM

Hmm. Try to remove the "C" bit from the extern line.
Unless you are telling the compiler that it should compile in 'C' mode it will generate C++ code...
The other thing, about "WinMain" missing gives me the impression that your project is a 'Windows' project, and not a 'simple' console project. Windows projects expect a different entrypoint "WinMain' instead of 'main()'...
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Cloudyeyes*


Reputation:

Re: Calling an external function in C

Posted 24 March 2010 - 04:20 AM

Alright, I removed the "C" bit and now it's only giving me that nasty WinMain error. You're right, though. I am using this on a Windows platform. The type of project is Visual C++ >> Empty Project.

Perhaps the better project option would be "Makefile Project?" (in Visual C++ >> Makefile Project) I'll have no idea what I'm doing, though. I notice it has such settings I have to put into it as, "Build command line:" "Clean commands" etc, etc, which leads me to believe it might be the right one to pick.
Was This Post Helpful? 0

#7 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Calling an external function in C

Posted 24 March 2010 - 05:50 AM

Try to make a new project, Visual C++, Win32, win32 console, and transfer your code there...
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Calling an external function in C

Posted 24 March 2010 - 07:45 AM

A full example:

//Test.cpp

#include <iostream>
using namespace std;

/*
void CalculateTaxesAndNetPay(float *fedtax, float gross, float deferred,
                float *statetax, float *ssitax, float *netpay){
                
        *fedtax = 15 * ((gross - deferred)/100); 
        *statetax = 7 * ((*fedtax)/100);
        *ssitax = 7.75*((gross - deferred)/100);
        *netpay = gross - *fedtax - *statetax - *ssitax - deferred;
}
*/

void externalFunction(){
	cout << "Hi! I'm from Test.cpp!" << endl;
}




//main.cpp

/*
extern void CalculateTaxesAndNetPay(float*, float, float,
				float*, float*, float*);
*/
extern void externalFunction();

int main(){
	//call function here
	externalFunction();
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1