Solved

Linking Multiple .C Modules and malloc()

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

New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now