Solved

Linking Multiple .C Modules and malloc()

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

707 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

12 Experts available now in Live!

Get 1:1 Help Now