Hi!

I have functions defined in ami.c and want to call them from friedman.c how do I link the 2?

Thanks
Ami

Recommended Answers

All 20 Replies

What compiler are you using?

Borland C++ 3.1

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;
}

Yea I know I need to declare.
Where exactly do I type:

C:\>bcc ami.c freidman.c

Thanks
Ami

At the command prompt. The path from the start button is something like Start->Programs->Accessories->Command Prompt.

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

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.

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

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;
}

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;
}

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. :)

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!!!

:(

test

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

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.

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

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.

  1. Gather all of your .c files in one directory
  2. Make sure that declarations between .c files are in .h files and the .h files are included in the .c files
  3. 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:

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.

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

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?

Corrrrrrect !!! also to never give up should be one of your qualitites.

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.