Linking 2 files.
Hi!
I have functions defined in ami.c and want to call them from friedman.c how do I link the 2?
Thanks
Ami
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
What compiler are you using?
Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
C:\>bcc ami.c freidman.c
But that'll only work if freidman.c includes declarations for the functions. Otherwise it won't compile because it doesn't know that the functions exist. You can declare them directly or use a header file. The better way is to use a header file. :)
/* ami.h */
#ifndef AMI
#define AMI
void function(void);
#endif
/* ami.c */
#include "ami.h"
void function(void)
{
/* ... */
}
/* freidman.c */
#include "ami.h"
int main(void)
{
function();
return 0;
}
Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
Yea I know I need to declare.
Where exactly do I type:
C:\>bcc ami.c freidman.c
Thanks
Ami
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
At the command prompt. The path from the start button is something like Start->Programs->Accessories->Command Prompt.
Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
Yea that's what I thought.
I tried it it didn't recognize bcc.
I tried to go to the specific file directory and even the BorlanC directory. None of them worked.
I must be doing something worng.
Thanks
Ami
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
I'm prolly forgetting the name of the executable. It might be bcc16 or bcc32 as well. Check the bin directory of where you installed the compiler and you'll see it.
Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
Ok!
Yes it IS bcc.
I put the 2 files in the same directory as BIN and then ran:
bcc file1.c file2.c
It opened a MS-DOS (Im using Win XP) window for a sec and closed. I assume it did something.
Now in file 2 I have this:
/* file2.c */
#include <stdio.h>
int main()
{
extern int i;
/* get_j(void) is in file1.c */
int get_j(void);
printf("i == %d\n", ++i);
printf("j == %d\n", get_j());
return 0;
}
It doesnt recognize "get_j". I diclared get_j in file1.c
Any idea why?
Thanks
Ami
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
Just a hunch, but try this and paste the error that you get.
/* file2.c */
#include <stdio.h>
int get_j(void);
int main()
{
extern int i;
printf("i == %d\n", ++i);
printf("j == %d\n", get_j());
return 0;
}
Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
hmmm
In file2.c when I run or link I get this:
Linking error:Undefined symbol _get_j in module FILE2.c
Linking error:Undefined symbol _i in module FILE2.c
In file1.c when I run I get this:
Linking error:Undefined symbol _main in module c0.ASM
here's file1.c:
/* file1.c */
int i = 10; /* global */
static int j = 20; /* private */
int get_j(void)
{
return j;
}
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
Hmm, you're sure that you're running bcc on both at the same time?
C:\>bcc file1.c file2.c
Unless my command line compiler skills are shot, that should work just fine. :)
Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
Here's what I did:
I put both files in the same directory (c:\BorlandC\BIN\).
Then I typed in THAT directory: bcc file1.c file2.c
What does the error in file1.c mean?
I'll be honist I took these files right off Chuck Allison's tutorial.
They're supposed to work.
I dont know what Im doing wrong. He doesn't explain how to link the 2 files!!!
:(
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
by the way...
What compiler for C do you recommend?
I'm using Borland C++ 3.1 which is a DOS based version.
There must be newer ones out there! I tried something called Block::Code but it's so confusing. The DOS one is just so simple!!
Thanks
Ami
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
What compiler for C do you recommend?
I like Visual Studio 2005. There's an Express version that's free, but it takes some getting used to if you haven't used a powerful IDE before. Another good one is Dev-C++, but the IDE is awkward to me. If you like the command line stuff, Borland gives away their 5.5 compiler.
All of them are sold as C++ environments, but they all do C just fine. :)I tried something called Block::Code but it's so confusing.
Code::Blocks is an IDE like Dev-C++. They both use the same compiler underneath, I think. Starting out, you prolly want something that's easy sqeezy to install. Dev-C++ and Visual C++ 2005 Express are that way, and unless you wanna do some bigtime customization, you can ignore most of the features.
Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
Back to the origional topic of this thread...
I decided to try something and it worked...
I added this:
#include "full path\name of file.c"
This worked like a charm.
I thought the whole point of linking is so I dont have to do that?
Does this make sense?
Thanks
Ami
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
I thought the whole point of linking is so I dont have to do that?
Ya, that's the point, more or less. :) The first thing you should do if possible is ditch the old compiler and get a newer one that's designed for your OS. That way you can error check your methodology without worrying if the compiler isn't playing nicely on your system. ;) Then as a test, do this.Gather all of your .c files in one directory
Make sure that declarations between .c files are in .h files and the .h files are included in the .c files
Compile and link all at once, but don't compile the .h files: C:\>compiler file1.c file2.c
Or if you get a coolio IDE, just add files to your project and click on the Build button. Mucho easier. :cheesy:
Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
I tried something called Block::Code but it's so confusing. The DOS one is just so simple!!
If you looking for free IDE's and dont want to spend money then Code::Blocks, is one of the best IDE's out there.
What do you find so confusing ??? Their site has all the resources and help required. Even i use Code Blocks, post what you find confusing and I will try to help you out if possible.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Thanks Inanna.
I'm working on getting Visual Studio. I already downloaded the the express version. I'm going to need more time on it to undersatnd how it works exactly.
S.O.S - you're right. I was just being lazy. If there's one thing I need to get used to in order to be succesful in the programming world is to be able to adjust and learn how to ditch the "old" softwares and storm ahead with the new ones, right?
I think once I learn one IDE properly I should be able to find all of them pretty easy to use.
Thanks guys
amishosh
Junior Poster in Training
59 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0