Solved

Linking Multiple .C Modules and malloc()

Posted on 1997-05-11
1
165 Views
Last Modified: 2006-11-17
I've been writing a program recently, and I found that it got too big to manage all in one .C file.  However I've run into a problem trying to link all of the files together.  I'm using Turbo C, and defined all of the .C files as part of a project.  But when I tried to link the program, (using the "Make" option) I got tons of "Undefined symbol '...' in function ..." errors.  I've got all my #include files defined in the main module (the one with the main() function), and I don't think the rest of the modules are seeing them.  But when I put the include files in all of the modules, I get "Duplicate symbol ..." (or something like that) errors.  How do I get the linker to recognise the include files for all of the modules?

Also, how would I go about allocating memory for a 250 element array of structures?  I tried:

  soldier = malloc(sizeof(soldier) * 250));

where 'soldier' was defined as:

  struct soldier *soldier;

I've probably made some pointer mistake, but I'm new to C and I'm still a little puzzled with all the different pointer operators (*, &, etc.).  Any help would be GREATLY appreciated as I can't go any farther with my program without clearing these problems up.  Thanks...
0
Comment
Question by:NeedHelp051197
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 10

Accepted Solution

by:
rbr earned 200 total points
ID: 1250336
I am not quite sure what your problem is, but I will give you some advice, how to work with multiple C-Files in one project.

First make a .inc-File with consists of all your definitions, functions, structures and global variables that are used in all of your C-files.

e.g.

File myinclude.inc

/********************************************************
 * list of defines
*********************************************************/
#define First_Definition       0
.
.
.
/***************************************************************
 * List of structures.
 ***************************************************************/
struct soldier {
 .
 .  
};
.
.
.
/******************************************************
 * List of global variables
 ******************************************************/
extern struct soldier *soldier;
.
.
.
/***********************************************************
 * List of prototypes
 ***********************************************************/
extern void first_function (struct soldier *);
.
.
.

Include this file in every file of your project.


soldier = malloc (sizeof(soldier)*250); you define a array of 250 pointers not of 250 elements of type struct soldier.

Write soldier = (struct soldier *)malloc(sizeof(struct soldier)*250);
or better
soldier = (struct soldier *)calloc(sizeof(struct soldier),250);


I hope this will solve your problems.


0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 21
why debugging a macro i s difficult 10 41
C#, VS15, StructLayout 1 135
why "." vs "->" 23 120
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

735 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