Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linking Multiple .C Modules and malloc()

Posted on 1997-05-11
1
Medium Priority
?
170 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 600 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

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

604 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