c++ 中的多重定义错误

发布于 2024-12-07 19:08:59 字数 12323 浏览 0 评论 0原文

这是我在 DevCpp (windows) 中编写的一些代码,现在我试图让它在 Linux 中运行,但没有取得太大成功。 这是源代码

screen.h

#ifndef SCREEN_H
#define SCREEN_H

#include <graphics.h>

class Screen
{
      private:
              int max_x,max_y,grid_size;
              int delta_x,delta_y;
              int origin_x,origin_y;
      public:
      // Default Constructor to Initialize the screen with the grid
      Screen(int xcoord=641, int ycoord=641, int grid=8)
      {
           //variable initialization
           max_x = xcoord;
           max_y = ycoord;
           grid_size = grid;

           delta_x = max_x / grid_size;
           delta_y = max_y / grid_size;

           origin_x = grid_size / 2 * delta_x;
           origin_y = grid_size / 2 * delta_y;

           //plotting the initial grid
           int gd,gm;
           initgraph(&gd,&gm,NULL);
            //draw the x component of the grid
             for(int i=0; i<max_x; i += delta_x)
             {
                     if( i != max_x / 2)
                         setcolor(GREEN);
                     else
                         setcolor(RED);
                    line(i,0,i,max_y);
             }
             //draw the y component of the grid
             for(int i=0; i<max_y; i += delta_y)
             {
                     if( i != max_y / 2)
                         setcolor(GREEN);
                     else
                         setcolor(RED);
                     line(0,i,max_x,i);
             }
             //mark the origin with a white dot to acertain its coordinates for future use
             putpixel(origin_x,origin_y,WHITE);
             std::cout<<origin_x<<"\t"<<origin_y<<"\n";
        }



      //Method prototypes
      void plot_pixel(int xcoord,int ycoord,int col=WHITE)
      {
        int l,t,r,b;
        l = origin_x + xcoord * delta_x;
        r = l + delta_x;
        b = origin_y - ycoord * delta_y;
        t = b - delta_y;
        setcolor(col);
        bar(l,t,r,b);
        setcolor(WHITE);
     }
};
#endif

circles.cpp

#include<iostream>
#include<cmath>
#include "screen.h"
using namespace std;

void draw_midpoint_circle(Screen ob, int radius);
void circlepointplot(Screen ob, int m, int n);
void trigonometric_circle(Screen ob, int radius);

int main() {
    Screen scr = Screen(641, 641, 16);
    int choice, rad;
    cout << "Menu\n 1. Midpoint Circle Drawing Algorithm\n 2. Bresenham's Circle Drawing Algorithm \n 3. Trigonometric Method\n";
    cin>>choice;
    cout << "Enter Radius \t";
    cin>>rad;

    switch (choice) {
        case 1:
            draw_midpoint_circle(scr, rad);
            break;
        case 2:
            draw_midpoint_circle(scr, rad);
            break;
        case 3:
            trigonometric_circle(scr, rad);
            break;
        default:
            cout << "Wrong Choice\n";
            break;
    }

    getch();
    return 0;
}

void trigonometric_circle(Screen ob, int radius) {
    double angle = 0.0;
    while (angle <= 360) {
        int dx = 641 / 16;
        int x = int(radius * cos(angle));
        int y = int(radius * sin(angle));
        angle = angle + 5;
        ob.plot_pixel(x, y, 0);
        cout << "Point Plotted  " << x << "\t" << y << endl;
        char buffer[50];
        sprintf(buffer, "%d,%d", x, y);
        outtextxy(320 + ((x + 1) * dx), 320 - ((y - 1) * dx), buffer);
        getch();
    }
}

void draw_midpoint_circle(Screen ob, int radius) {
    float dp;
    int x, y;
    x = 0;
    y = radius;
    dp = 1 - radius;
    while (x < y) {
        circlepointplot(ob, x, y);
        if (dp < 0)
            dp = dp + 2 * x + 3;
        else {
            dp = dp + 2 * (x - y) + 5;
            y--;
        }

        x++;
        circlepointplot(ob, x, y);
    }
}

void circlepointplot(Screen ob, int m, int n) {
    ob.plot_pixel(m, n, 0);
    ob.plot_pixel(n, m, 0);
    ob.plot_pixel(m, -n, 0);
    ob.plot_pixel(n, -m, 0);
    ob.plot_pixel(-m, n, 0);
    ob.plot_pixel(-n, m, 0);
    ob.plot_pixel(-m, -n, 0);
    ob.plot_pixel(-n, -m, 0);

    cout << "Point Plotted" << m << "\t" << n << endl;
    cout << "Point Plotted" << n << "\t" << m << endl;
    cout << "Point Plotted" << m << "\t" << -n << endl;
    cout << "Point Plotted" << n << "\t" << -m << endl;
    cout << "Point Plotted" << -m << "\t" << n << endl;
    cout << "Point Plotted" << -n << "\t" << m << endl;
    cout << "Point Plotted" << -m << "\t" << -n << endl;
    cout << "Point Plotted" << -n << "\t" << -m << endl;
    int dx = 641 / 16;
    char buffer[50];
    sprintf(buffer, "%d,%d", m, n);
    outtextxy(320 + ((m + 1) * dx), 320 - ((n - 1) * dx), buffer);
    getch();

}

我正在使用 graphics.h 适用于 Linux。基本程序运行良好。 我得到的错误是

g++ -c screen.cpp -o screen.o
g++ -c circles.cpp -o circles.o
g++ screen.o circles.o -o "circle.exe" -lgraph
circles.o:(.bss+0x0): multiple definition of `screen'
screen.o:(.bss+0x0): first defined here
circles.o:(.bss+0x8): multiple definition of `Font_surface'
screen.o:(.bss+0x8): first defined here
circles.o:(.bss+0x10): multiple definition of `_fgcolor'
screen.o:(.bss+0x10): first defined here
circles.o:(.bss+0x14): multiple definition of `_bgcolor'
screen.o:(.bss+0x14): first defined here
circles.o:(.bss+0x18): multiple definition of `_fontcolor'
screen.o:(.bss+0x18): first defined here
circles.o:(.bss+0x1c): multiple definition of `_pid'
screen.o:(.bss+0x1c): first defined here
circles.o:(.bss+0x20): multiple definition of `CP'
screen.o:(.bss+0x20): first defined here
circles.o:(.bss+0x40): multiple definition of `InternalFont'
screen.o:(.bss+0x40): first defined here
circles.o:(.bss+0x850): multiple definition of `TP'
screen.o:(.bss+0x850): first defined here
circles.o:(.bss+0x860): multiple definition of `_last_arc'
screen.o:(.bss+0x860): first defined here
circles.o:(.bss+0x878): multiple definition of `_internal_linestyle'
screen.o:(.bss+0x878): first defined here
circles.o:(.bss+0x888): multiple definition of `_scanlist'
screen.o:(.bss+0x888): first defined here
collect2: ld returned 1 exit status

我做错了什么,如何让它工作?

将代码移动到类中后更新了错误。

/tmp/ccB2RO2Q.o: In function `main':
circles.cpp:(.text+0x111): undefined reference to `grgetch'
/tmp/ccB2RO2Q.o: In function `trigonometric_circle(Screen, int)':
circles.cpp:(.text+0x242): undefined reference to `outtextxy'
circles.cpp:(.text+0x247): undefined reference to `grgetch'
/tmp/ccB2RO2Q.o: In function `circlepointplot(Screen, int, int)':
circles.cpp:(.text+0x6f2): undefined reference to `outtextxy'
circles.cpp:(.text+0x6f7): undefined reference to `grgetch'
/tmp/ccB2RO2Q.o: In function `Screen::Screen(int, int, int)':
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0xd0): undefined reference to `initgraph'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0xf7): undefined reference to `setcolor'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x103): undefined reference to `setcolor'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x11c): undefined reference to `line'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x15e): undefined reference to `setcolor'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x16a): undefined reference to `setcolor'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x182): undefined reference to `line'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x1b9): undefined reference to `putpixel'
/tmp/ccB2RO2Q.o: In function `Screen::plot_pixel(int, int, int)':
circles.cpp:(.text._ZN6Screen10plot_pixelEiii[Screen::plot_pixel(int, int, int)]+0x6d): undefined reference to `setcolor'
circles.cpp:(.text._ZN6Screen10plot_pixelEiii[Screen::plot_pixel(int, int, int)]+0x80): undefined reference to `bar'
circles.cpp:(.text._ZN6Screen10plot_pixelEiii[Screen::plot_pixel(int, int, int)]+0x8a): undefined reference to `setcolor'
collect2: ld returned 1 exit status

这是graphics.h 文件,它引用了 SDL_Image *screen

/* libgraph - TurboC graphics API on GNU/Linux
 * graphics.h: Core initialization and configuration functions
 * 
 * Copyright (C) 2003  Faraz Shahbazker
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 *  MA 02111-1307, USA
 *
 * Author:  Faraz Shahbazker <[email protected]>
 */

/* Graphic functions using SDL */

#ifndef GRAPHICS_H
#define GRAPHICS_H 1


#include <SDL/SDL.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


#ifdef __cplusplus
extern "C" 
{

#endif      /* __cplusplus */


/* graphic drivers */
enum _driver{DETECT=0, USER, VGA=9};
enum graphics_modes{VGALO=0, VGAMED, VGAHI, VGAMAX, VGA640, VGA800, VGA1024, USERMODE};


/* 16 colors */
enum _color{BLACK=0, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY,LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE};



/* global variables */
SDL_Surface *screen;        //main drawing screen
SDL_Surface *Font_surface;  //font screen 
Uint32 _fgcolor, _bgcolor, _fontcolor;  //global color numbers
pid_t _pid;         //Don't bother with this 



/* function prototypes */
void initgraph(int *graphdriver,int *graphmode,char *pathtodriver);
void closegraph(void);
void setgraphmode(int gmode);
int  getgraphmode(void);
void restorecrtmode(void);
int  getmaxx(void);
int  getmaxy(void);
void putpixel(int x, int y, int color);
int  getpixel(int, int);
void setbkcolor(int color);
int  getbkcolor(void);
void setcolor(int color);
int  getcolor(void);
int getmaxcolor(void);
char* getdrivername(void);
char* getmodename(int mode_number);
int getmaxmode(void);
void detectgraph(int* graphdriver, int* graphmode);
void getmoderange(int graphdriver, int* lomode, int* himode);
int delay(float);
void setfontcolor(int color);
int getfontcolor(void);





/*** library specific functions - not for users ***/
void initialize_settings (void);
void mappixel(int, int);     //marks a pixel without updating screen
void clippixel(int *, int *); /* Clip pixel (x,y) to current
                                 screen size*/
void mapword(int,int,int);
void mapvword(int,int,int);
int colorrev(const Uint8);   // maps 0..255 8-bit color to 0..15 TC color
Uint8 colortrans(const int); // maps 0..15 TC color to 0..255 8-bit color
void ctrlbreak(void);        // To detect user interrupt    
void inthandler(int);        // clean up on user interrupt
void safe_update(int top, int left, int right, int bottom);
    /* update screen within boundary */

#define CHECK_INITIALIZATION\
    if (!getenv("LIBGRAPHICS_ACTIVE")) \
    { \
        fprintf(stderr, "*** The graphics system has not been initialized!\n"); \
        fprintf(stderr, "*** Call initgraph() before trying to use graphics functions.\n"); \
        exit(-1); \
    }

struct {int x;int y;}CP;

#include "grtext.h"
#include "shapes.h"
#include "polygon.h"

#ifdef __cplusplus
}
#endif      /* __cplusplus */


#endif      /* LIBGRAPH_H */

Here's some code that I wrote in DevCpp (windows), now I'm trying to get it to run in linux without much success.
Here is the source code

screen.h

#ifndef SCREEN_H
#define SCREEN_H

#include <graphics.h>

class Screen
{
      private:
              int max_x,max_y,grid_size;
              int delta_x,delta_y;
              int origin_x,origin_y;
      public:
      // Default Constructor to Initialize the screen with the grid
      Screen(int xcoord=641, int ycoord=641, int grid=8)
      {
           //variable initialization
           max_x = xcoord;
           max_y = ycoord;
           grid_size = grid;

           delta_x = max_x / grid_size;
           delta_y = max_y / grid_size;

           origin_x = grid_size / 2 * delta_x;
           origin_y = grid_size / 2 * delta_y;

           //plotting the initial grid
           int gd,gm;
           initgraph(&gd,&gm,NULL);
            //draw the x component of the grid
             for(int i=0; i<max_x; i += delta_x)
             {
                     if( i != max_x / 2)
                         setcolor(GREEN);
                     else
                         setcolor(RED);
                    line(i,0,i,max_y);
             }
             //draw the y component of the grid
             for(int i=0; i<max_y; i += delta_y)
             {
                     if( i != max_y / 2)
                         setcolor(GREEN);
                     else
                         setcolor(RED);
                     line(0,i,max_x,i);
             }
             //mark the origin with a white dot to acertain its coordinates for future use
             putpixel(origin_x,origin_y,WHITE);
             std::cout<<origin_x<<"\t"<<origin_y<<"\n";
        }



      //Method prototypes
      void plot_pixel(int xcoord,int ycoord,int col=WHITE)
      {
        int l,t,r,b;
        l = origin_x + xcoord * delta_x;
        r = l + delta_x;
        b = origin_y - ycoord * delta_y;
        t = b - delta_y;
        setcolor(col);
        bar(l,t,r,b);
        setcolor(WHITE);
     }
};
#endif

circles.cpp

#include<iostream>
#include<cmath>
#include "screen.h"
using namespace std;

void draw_midpoint_circle(Screen ob, int radius);
void circlepointplot(Screen ob, int m, int n);
void trigonometric_circle(Screen ob, int radius);

int main() {
    Screen scr = Screen(641, 641, 16);
    int choice, rad;
    cout << "Menu\n 1. Midpoint Circle Drawing Algorithm\n 2. Bresenham's Circle Drawing Algorithm \n 3. Trigonometric Method\n";
    cin>>choice;
    cout << "Enter Radius \t";
    cin>>rad;

    switch (choice) {
        case 1:
            draw_midpoint_circle(scr, rad);
            break;
        case 2:
            draw_midpoint_circle(scr, rad);
            break;
        case 3:
            trigonometric_circle(scr, rad);
            break;
        default:
            cout << "Wrong Choice\n";
            break;
    }

    getch();
    return 0;
}

void trigonometric_circle(Screen ob, int radius) {
    double angle = 0.0;
    while (angle <= 360) {
        int dx = 641 / 16;
        int x = int(radius * cos(angle));
        int y = int(radius * sin(angle));
        angle = angle + 5;
        ob.plot_pixel(x, y, 0);
        cout << "Point Plotted  " << x << "\t" << y << endl;
        char buffer[50];
        sprintf(buffer, "%d,%d", x, y);
        outtextxy(320 + ((x + 1) * dx), 320 - ((y - 1) * dx), buffer);
        getch();
    }
}

void draw_midpoint_circle(Screen ob, int radius) {
    float dp;
    int x, y;
    x = 0;
    y = radius;
    dp = 1 - radius;
    while (x < y) {
        circlepointplot(ob, x, y);
        if (dp < 0)
            dp = dp + 2 * x + 3;
        else {
            dp = dp + 2 * (x - y) + 5;
            y--;
        }

        x++;
        circlepointplot(ob, x, y);
    }
}

void circlepointplot(Screen ob, int m, int n) {
    ob.plot_pixel(m, n, 0);
    ob.plot_pixel(n, m, 0);
    ob.plot_pixel(m, -n, 0);
    ob.plot_pixel(n, -m, 0);
    ob.plot_pixel(-m, n, 0);
    ob.plot_pixel(-n, m, 0);
    ob.plot_pixel(-m, -n, 0);
    ob.plot_pixel(-n, -m, 0);

    cout << "Point Plotted" << m << "\t" << n << endl;
    cout << "Point Plotted" << n << "\t" << m << endl;
    cout << "Point Plotted" << m << "\t" << -n << endl;
    cout << "Point Plotted" << n << "\t" << -m << endl;
    cout << "Point Plotted" << -m << "\t" << n << endl;
    cout << "Point Plotted" << -n << "\t" << m << endl;
    cout << "Point Plotted" << -m << "\t" << -n << endl;
    cout << "Point Plotted" << -n << "\t" << -m << endl;
    int dx = 641 / 16;
    char buffer[50];
    sprintf(buffer, "%d,%d", m, n);
    outtextxy(320 + ((m + 1) * dx), 320 - ((n - 1) * dx), buffer);
    getch();

}

I'm using graphics.h for linux. Basic programs run fine.
The errors that I get are

g++ -c screen.cpp -o screen.o
g++ -c circles.cpp -o circles.o
g++ screen.o circles.o -o "circle.exe" -lgraph
circles.o:(.bss+0x0): multiple definition of `screen'
screen.o:(.bss+0x0): first defined here
circles.o:(.bss+0x8): multiple definition of `Font_surface'
screen.o:(.bss+0x8): first defined here
circles.o:(.bss+0x10): multiple definition of `_fgcolor'
screen.o:(.bss+0x10): first defined here
circles.o:(.bss+0x14): multiple definition of `_bgcolor'
screen.o:(.bss+0x14): first defined here
circles.o:(.bss+0x18): multiple definition of `_fontcolor'
screen.o:(.bss+0x18): first defined here
circles.o:(.bss+0x1c): multiple definition of `_pid'
screen.o:(.bss+0x1c): first defined here
circles.o:(.bss+0x20): multiple definition of `CP'
screen.o:(.bss+0x20): first defined here
circles.o:(.bss+0x40): multiple definition of `InternalFont'
screen.o:(.bss+0x40): first defined here
circles.o:(.bss+0x850): multiple definition of `TP'
screen.o:(.bss+0x850): first defined here
circles.o:(.bss+0x860): multiple definition of `_last_arc'
screen.o:(.bss+0x860): first defined here
circles.o:(.bss+0x878): multiple definition of `_internal_linestyle'
screen.o:(.bss+0x878): first defined here
circles.o:(.bss+0x888): multiple definition of `_scanlist'
screen.o:(.bss+0x888): first defined here
collect2: ld returned 1 exit status

What am I doing wrong, how do I get this to work?

Updated errors after moving the code into the class.

/tmp/ccB2RO2Q.o: In function `main':
circles.cpp:(.text+0x111): undefined reference to `grgetch'
/tmp/ccB2RO2Q.o: In function `trigonometric_circle(Screen, int)':
circles.cpp:(.text+0x242): undefined reference to `outtextxy'
circles.cpp:(.text+0x247): undefined reference to `grgetch'
/tmp/ccB2RO2Q.o: In function `circlepointplot(Screen, int, int)':
circles.cpp:(.text+0x6f2): undefined reference to `outtextxy'
circles.cpp:(.text+0x6f7): undefined reference to `grgetch'
/tmp/ccB2RO2Q.o: In function `Screen::Screen(int, int, int)':
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0xd0): undefined reference to `initgraph'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0xf7): undefined reference to `setcolor'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x103): undefined reference to `setcolor'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x11c): undefined reference to `line'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x15e): undefined reference to `setcolor'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x16a): undefined reference to `setcolor'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x182): undefined reference to `line'
circles.cpp:(.text._ZN6ScreenC2Eiii[_ZN6ScreenC5Eiii]+0x1b9): undefined reference to `putpixel'
/tmp/ccB2RO2Q.o: In function `Screen::plot_pixel(int, int, int)':
circles.cpp:(.text._ZN6Screen10plot_pixelEiii[Screen::plot_pixel(int, int, int)]+0x6d): undefined reference to `setcolor'
circles.cpp:(.text._ZN6Screen10plot_pixelEiii[Screen::plot_pixel(int, int, int)]+0x80): undefined reference to `bar'
circles.cpp:(.text._ZN6Screen10plot_pixelEiii[Screen::plot_pixel(int, int, int)]+0x8a): undefined reference to `setcolor'
collect2: ld returned 1 exit status

Here's the graphics.h file it has a reference to an SDL_Image *screen

/* libgraph - TurboC graphics API on GNU/Linux
 * graphics.h: Core initialization and configuration functions
 * 
 * Copyright (C) 2003  Faraz Shahbazker
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 *  MA 02111-1307, USA
 *
 * Author:  Faraz Shahbazker <[email protected]>
 */

/* Graphic functions using SDL */

#ifndef GRAPHICS_H
#define GRAPHICS_H 1


#include <SDL/SDL.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


#ifdef __cplusplus
extern "C" 
{

#endif      /* __cplusplus */


/* graphic drivers */
enum _driver{DETECT=0, USER, VGA=9};
enum graphics_modes{VGALO=0, VGAMED, VGAHI, VGAMAX, VGA640, VGA800, VGA1024, USERMODE};


/* 16 colors */
enum _color{BLACK=0, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY,LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE};



/* global variables */
SDL_Surface *screen;        //main drawing screen
SDL_Surface *Font_surface;  //font screen 
Uint32 _fgcolor, _bgcolor, _fontcolor;  //global color numbers
pid_t _pid;         //Don't bother with this 



/* function prototypes */
void initgraph(int *graphdriver,int *graphmode,char *pathtodriver);
void closegraph(void);
void setgraphmode(int gmode);
int  getgraphmode(void);
void restorecrtmode(void);
int  getmaxx(void);
int  getmaxy(void);
void putpixel(int x, int y, int color);
int  getpixel(int, int);
void setbkcolor(int color);
int  getbkcolor(void);
void setcolor(int color);
int  getcolor(void);
int getmaxcolor(void);
char* getdrivername(void);
char* getmodename(int mode_number);
int getmaxmode(void);
void detectgraph(int* graphdriver, int* graphmode);
void getmoderange(int graphdriver, int* lomode, int* himode);
int delay(float);
void setfontcolor(int color);
int getfontcolor(void);





/*** library specific functions - not for users ***/
void initialize_settings (void);
void mappixel(int, int);     //marks a pixel without updating screen
void clippixel(int *, int *); /* Clip pixel (x,y) to current
                                 screen size*/
void mapword(int,int,int);
void mapvword(int,int,int);
int colorrev(const Uint8);   // maps 0..255 8-bit color to 0..15 TC color
Uint8 colortrans(const int); // maps 0..15 TC color to 0..255 8-bit color
void ctrlbreak(void);        // To detect user interrupt    
void inthandler(int);        // clean up on user interrupt
void safe_update(int top, int left, int right, int bottom);
    /* update screen within boundary */

#define CHECK_INITIALIZATION\
    if (!getenv("LIBGRAPHICS_ACTIVE")) \
    { \
        fprintf(stderr, "*** The graphics system has not been initialized!\n"); \
        fprintf(stderr, "*** Call initgraph() before trying to use graphics functions.\n"); \
        exit(-1); \
    }

struct {int x;int y;}CP;

#include "grtext.h"
#include "shapes.h"
#include "polygon.h"

#ifdef __cplusplus
}
#endif      /* __cplusplus */


#endif      /* LIBGRAPH_H */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救赎№ 2024-12-14 19:08:59

Graphics.h 包含以下几行:

/* global variables */ 
SDL_Surface *screen;        //main drawing screen 
SDL_Surface *Font_surface;  //font screen  
Uint32 _fgcolor, _bgcolor, _fontcolor;  //global color numbers 
pid_t _pid;         //Don't bother with this  

这有点奇怪,但是这个 h 文件创建了全局变量。这意味着,如果您将此文件包含到多个 .cpp 文件中,则会出现多个定义错误。我不知道为什么这个h文件是这样写的,只有当graphics.h只包含在一个cpp文件中时,程序才会被链接。如果您可以更改此文件,请在每个全局变量之前添加 extern 关键字,并在某些 .cpp 文件中创建这些变量(如果需要)。

graphics.h contains the following lines:

/* global variables */ 
SDL_Surface *screen;        //main drawing screen 
SDL_Surface *Font_surface;  //font screen  
Uint32 _fgcolor, _bgcolor, _fontcolor;  //global color numbers 
pid_t _pid;         //Don't bother with this  

This is a bit strange, but this h-file creates global variables. This means, if you include this file to more than one .cpp files, you have multiple definition error. I don't know why this h-file is written by such way, the program will be linked only if graphics.h is included to only one cpp file. If your can change this file, add extern keyword before every global variable, and create these variables (if necessary) in some .cpp file.

月依秋水 2024-12-14 19:08:59

您的第一条错误消息是

circles.o:(.bss+0x0): ‘screen’的多重定义

在我写这篇文章的代码中,没有任何东西叫做“screen”。

因此,问题似乎已经解决了。


编辑:现在您已经添加了(其他人的)graphics.h 的内容,作者对以下内容的无知是导致问题的原因:

C++98 §7.5/7
包含花括号括起来的声明序列的链接规范的形式确实
不影响所包含的声明是否是定义(3.1)。

所以指针变量声明有定义。

不幸的是,看来不仅是该标头的作者,而且当前的 C++ 标准也弄错了

C++11 §7.5/7:
直接包含在链接规范中的声明被视为包含
extern 说明符 (7.1.1),用于确定声明的链接
名称以及是否是定义。

根据该规范文本,就好像每个指针变量都被声明为
extern 因此会
只是一个声明(不是定义)。然而,即使在 C++11 中,非规范示例
显示了意图,它是一个定义。

话虽如此,这对我来说也是新闻,而且我是这方面的老手了。似乎只是
标准中毫无意义的怪癖,不必要的陷阱。但可能是因为在 C++ 中
没有其他方法可以表达对象的纯声明与定义。

干杯&呵呵,,

Your first error message is

circles.o:(.bss+0x0): multiple definition of `screen'

In your code, as I write this, there is nothing called "screen".

Hence, the problem appears to be solved already.


EDIT: now that you have added the contents of (someone else's) graphics.h, the author's ignorance of the following is the cause of the problem:

C++98 §7.5/7
The form of linkage specification that contains a braced-enclosed declaration-seq does
not affect whether the contained declarations are definitions or not (3.1).

So the pointer variable declarations there are definitions.

Unfortunately it appears that not only the author of that header, but also the current C++ standard got it wrong:

C++11 §7.5/7:
A declaration directly contained in a linkage-specification is treated as if it contains
the extern specifier (7.1.1) for the purpose of determining the linkage of the declared
name and whether it is a definition.

According to that normative text it is as if each pointer variable was declared as
extern and thus would
be only a declaration (not a definition). However, even in C++11 the non-normative example
shows the intent, that it is a definition.

All that said, this was news to me too, and I'm an old hand at this. It seems to be just
a meaningless quirk in the standard, a needless gotcha. But possibly it's because in C++
there is no other way to express pure declaration versus definition for an object.

Cheers & hth.,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文