codeblock中C++调用C代码

#ifdef __cplusplus

extern "c" {

#endif

 

。。。。。。。

 

#ifdef __cplusplus

}

#endif

 

这一对声明块的出现主要是不同语言的编译器对函数名的处理不一样。C编译器不会改变目标文件中的函数名,而C++编译器会根据C++的语言规则对函数名进行修饰。

在C++中,调用一个函数时,编译器会先对这个函数名进行修饰,生成目标符号名,然后到工程包含的符号表中查找这个修饰后的符号名,找到了就会调用。而因为C编译器不会对C函数进行修饰,所以C++编译器在用修饰后的符号名在符号表中查找时,就会找不到对象。

这一对声明块就是告诉C++编译器,不要对块内的内容进行名字修饰,而是采用C的方式生成符号名。这样C++编译器就会用C编译器处理方式处理过的符号名到符号表中去查找,就能找到了。

 

codeblock中的编译器分别对C和CPP代码设置了编译器,比如我的GNU的设置是GCC和G++。

当一个工程中既包含CPP文件也包含C文件时,codeblock自动对C文件用GCC编译,对CPP文件用G++编译。

这样一来,当从CPP文件调用C文件的程序时,就会因为不同编译器生成的函数名不一致而发生找不到函数的情况。

 

解决方案有两个:

1、把C编译器也设置成G++,从而所有的函数名都采用C++的名称修饰。

2、从CPP引用C头文件或者函数时加上 上面的声明块。

3、在C头文件中加上 上面的声明块。

 

如果要从C++代码中调用C源代码,需要在引用C头文件时,用

 

#ifdef __cplusplus

extern "c" {

#endif

 

#include "myCHead.h"

 

#ifdef __cplusplus

}

#endif

 

在C头文件中把函数声明放在

 

#ifdef __cplusplus

extern "c" {

#endif

 

int hello();

 

#ifdef __cplusplus

}

#endif

 

这样,在C++代码中include这个头文件时,就不需要加上这两个预编译块。

以下是使用 Code::Blocks IDE 编写的 C语言围棋代码,包括吃子和禁手规则。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define BOARD_SIZE 19 #define EMPTY 0 #define BLACK 1 #define WHITE 2 int board[BOARD_SIZE][BOARD_SIZE]; int current_player = BLACK; void print_board(){ printf(" "); for(int i=0;i<BOARD_SIZE;++i){ printf("%c ", 'A'+i); } printf("\n"); for(int i=0;i<BOARD_SIZE;++i){ printf("%2d", i+1); for(int j=0;j<BOARD_SIZE;++j){ if(board[i][j] == EMPTY){ printf(" +"); } else if(board[i][j] == BLACK){ printf(" X"); } else{ printf(" O"); } } printf("\n"); } } int is_within_board(int x, int y){ return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE; } int is_surrounded(int x, int y){ if(!is_within_board(x, y) || board[x][y] != EMPTY){ return 0; } int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int i=0;i<4;++i){ int nx = x + dx[i], ny = y + dy[i]; if(is_within_board(nx, ny) && board[nx][ny] == EMPTY){ return 0; } } return 1; } int is_legal_move(int x, int y){ if(!is_within_board(x, y) || board[x][y] != EMPTY){ return 0; } int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int i=0;i<4;++i){ int nx = x + dx[i], ny = y + dy[i]; if(is_within_board(nx, ny) && board[nx][ny] == current_player){ if(is_surrounded(nx, ny)){ return 1; } } } return 0; } int is_ko(int x, int y){ int tmp_board[BOARD_SIZE][BOARD_SIZE]; memcpy(tmp_board, board, sizeof(board)); board[x][y] = current_player; int captured = 0; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int i=0;i<4;++i){ int nx = x + dx[i], ny = y + dy[i]; if(is_within_board(nx, ny) && board[nx][ny] == 3 - current_player){ if(is_surrounded(nx, ny)){ captured += 1; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int j=0;j<4;++j){ int nnx = nx + dx[j], nny = ny + dy[j]; if(is_within_board(nnx, nny) && board[nnx][nny] == current_player){ if(is_surrounded(nnx, nny)){ board[nx][ny] = EMPTY; } } } } } } int res = 0; if(captured == 1){ int cnt = 0; for(int i=0;i<BOARD_SIZE;++i){ for(int j=0;j<BOARD_SIZE;++j){ if(board[i][j] != tmp_board[i][j]){ cnt += 1; } } } res = cnt == 1; } memcpy(board, tmp_board, sizeof(board)); return res; } void do_move(int x, int y){ if(is_legal_move(x, y)){ if(is_ko(x, y)){ printf("Illegal move: ko rule\n"); return; } board[x][y] = current_player; int captured = 0; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int i=0;i<4;++i){ int nx = x + dx[i], ny = y + dy[i]; if(is_within_board(nx, ny) && board[nx][ny] == 3 - current_player){ if(is_surrounded(nx, ny)){ captured += 1; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int j=0;j<4;++j){ int nnx = nx + dx[j], nny = ny + dy[j]; if(is_within_board(nnx, nny) && board[nnx][nny] == current_player){ if(is_surrounded(nnx, nny)){ board[nx][ny] = EMPTY; } } } } } } if(captured == 0){ current_player = 3 - current_player; } } else{ printf("Illegal move\n"); } } int main(){ memset(board, EMPTY, sizeof(board)); while(1){ print_board(); printf("Current player: %s\n", current_player == BLACK ? "Black(X)" : "White(O)"); printf("Enter move (e.g. A1): "); char move[10]; scanf("%s", move); if(strcmp(move, "q") == 0 || strcmp(move, "quit") == 0){ break; } if(strlen(move) != 2){ printf("Invalid input\n"); continue; } int x = move[1] - '1', y = move[0] - 'A'; do_move(x, y); } return 0; } ``` 在这个代码,我们使用了一个二维数组来表示围棋棋盘,其 `EMPTY`, `BLACK` 和 `WHITE` 分别代表空位、黑子和白子。我们还定义了一个 `current_player` 变量来表示当前玩家。在 `print_board()` 函数,我们打印当前棋盘的状态。 接下来我们定义了一些辅助函数,用于判断一个位置是否在棋盘内、是否被包围、是否可以合法落子等。在 `is_legal_move()` 函数,我们首先判断目标位置是否在棋盘内且为空位,然后检查该位置周围是否存在己方棋子被包围的情况,如果存在,则说明该位置可以合法落子。 在 `is_ko()` 函数,我们先判断落子后是否会有对手的棋子被提掉,如果存在,则进一步判断这个提掉的棋子是否唯一。如果唯一,则说明这是一个禁着点,不能落子,否则可以落子。 在 `do_move()` 函数,我们首先检查目标位置是否可以合法落子,如果合法,则更新棋盘状态,并检查是否有对手的棋子被提掉。如果没有,则交换当前玩家。 最后,在主函数,我们循环读入玩家的落子,并调用 `do_move()` 函数来更新棋盘状态。如果玩家输入 `q` 或 `quit`,则退游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值