Solved

Linking Multiple .C Modules and malloc()

Posted on 1997-05-11
1
166 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

Technology Partners: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

732 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