[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Problem...



Johan Andersson <sslug@sslug> wrote:

> `int rekt(...)'
>         mattematic3.cc:130: warning: implicit declaration of function
>         /tmp/cca017505.o(.text+0x4e4): undefined reference to `linjen2'
> 
> Det fungerar perfekt om jag tar bort dessa delarna av programmet, men
> det vill jag inte. Problemet är att dessa fel inte fanns när jag
> includade filerna, men det började när jag länkade ihopa dom. Jag är
> inte så duktig på att skriva varesig Makefiles eller C++ kod, men jag
> vill lära mig.

Mit svenske lader en del tilbage at ønske. Jeg håber, at jeg har fået
rigtig fat i dit indlæg!

Problemet er, at compileren skal kende til dine funktioner når de
kaldes. Da du brugte #include til at kæde programmet sammen var der
ingen problemer fordi du implementerede funktionerne inden du brugte
dem.

Når du vil linke funktionerne sammen (en udmærket idé), er det
nødvendigt med "prototyper". Det er en beskrivelse for hver funktion,
der fortæller hvad den returnerer og hvilke argumenter den bruger. De
placeres bedst i en "header fil". Jeg håber, at følgende lille
eksempel kan hjælpe på vej (ikke afprøvet):


file1.h:
 int func1(int a, int b);

file1.c:
 int func1(int a, int b) {
   return a+b;
 }


file2.h:
 int func2(int a, int b);

file2.c:
 int func2(int a, int b) {
   return a*b;
 }


main.c:
 #include <stdlib.h>
 #include <stdio.h>

 #include "file1.h"
 #include "file2.h"

 int main(int argc, char *argv[]) {
  int i=1;
  int j=2;
  int k, l;

  k = func1(i,j);
  l = func2(i,j);
  printf("%d %d\n", k, l);
  return 0;
 }


Compiler det cirka således:

 $ gcc file1.c file2.c main.c -o program

Bemærk, at man skal #include header filen til de moduler, man bruger
funktioner fra.

-- 
Regards, Anders                         If a Microsoft product
(address is valid)                      fails, who do you sue?


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *