鱼C论坛

 找回密码
 立即注册
查看: 1300|回复: 7

[已解决]多个C文件如何能执行通过

[复制链接]
发表于 2020-5-23 21:14:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
mul_a.cpp
  1. #include<stdio.h>
  2. #include "mul_b.h"

  3. int main()
  4. {
  5.    int hard = add(1,1);
  6.    printf("hard=%d\n",hard);
  7.    return 0;
  8. }
复制代码

mul_b.cpp
  1. #include<stdio.h>
  2. int add(int a,int b)
  3. {
  4.     return a+b;
  5. }
复制代码

mul_b.h
  1. int add(int a,int b);
复制代码


执行mul_a.cpp:
> Executing task: "g++ -O2" -g d:\Tools\Vsd-cpp\test\mul_a.cpp -o d:\Tools\Vsd-cpp\test/mul_a.exe -ggdb3 -Wall -static-libgcc -std=c++17 -Wno-format -fexec-charset=GBK -finput-charset=UTF-8 <

d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\ADMINI~1\AppData\Local\Temp\ccTiwuC3.o: in function `main':
d:/Tools/Vsd-cpp/test/mul_a.cpp:6: undefined reference to `add(int, int)'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.
最佳答案
2020-5-23 21:36:13
对于多文件编译,首先保证头文件与函数定义文件路径正确;
如果是Windows用户请参考你的IDE如何建立C/C++工程;
Linux用户直接
g++ mul_a.cpp mul_b.cpp -o MUL.out
./MUL.out
即可执行。
=====================================================================
头文件写法建议格式,xx.c一般只写函数、算法等具体实现
#ifndef _MUL
#define _MUL
//写入要用的头文件,结构体,类命等;但是,其中一定不要有实例!!!
#include <stdio.h>
//函数声明
int add(int,int);

#endif
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-23 21:18:01 | 显示全部楼层
试试改成
mul_a.cpp
  1. #include<stdio.h>
  2. #include "mul_b.h"

  3. extern int(*add)(int,int)
  4. int main()
  5. {
  6.    int hard = add(1,1);
  7.    printf("hard=%d\n",hard);
  8.    return 0;
  9. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-23 21:28:40 | 显示全部楼层

这样编译可以通过,执行和之前一样:d:/Tools/Vsd-cpp/test/mul_a.cpp:6: undefined reference to `add(int, int)'
  1. #include<stdio.h>
  2. //#include "mul_b.h"

  3. extern int(*add)(int,int);
  4. int main()
  5. {
  6.    int hard = add(1,1);
  7.    printf("hard=%d\n",hard);
  8.    return 0;
  9. }
复制代码


如果两个同时打开,就会编译阶段报错(一看函数定义成了不同类型,当然同一类型的话,就是重复定义了):'int (* add)(int, int)' redeclared as different kind of entitygcc
  1. #include<stdio.h>
  2. #include "mul_b.h"

  3. extern int(*add)(int,int);
  4. int main()
  5. {
  6.    int hard = add(1,1);
  7.    printf("hard=%d\n",hard);
  8.    return 0;
  9. }
复制代码

虽然还是通不过,谢谢啦。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-23 21:31:49 | 显示全部楼层
java2python 发表于 2020-5-23 21:28
这样编译可以通过,执行和之前一样:d:/Tools/Vsd-cpp/test/mul_a.cpp:6: undefined reference to `add(i ...

试试
  1. #include<stdio.h>
  2. #include "mul_b.h"

  3. extern int add(int,int);
  4. int main()
  5. {
  6.    int hard = add(1,1);
  7.    printf("hard=%d\n",hard);
  8.    return 0;
  9. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-23 21:36:13 | 显示全部楼层    本楼为最佳答案   
对于多文件编译,首先保证头文件与函数定义文件路径正确;
如果是Windows用户请参考你的IDE如何建立C/C++工程;
Linux用户直接
g++ mul_a.cpp mul_b.cpp -o MUL.out
./MUL.out
即可执行。
=====================================================================
头文件写法建议格式,xx.c一般只写函数、算法等具体实现
#ifndef _MUL
#define _MUL
//写入要用的头文件,结构体,类命等;但是,其中一定不要有实例!!!
#include <stdio.h>
//函数声明
int add(int,int);

#endif
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-23 21:41:57 | 显示全部楼层

这样还是不行,其实我试了很多次了,应该只能是像5楼说的,既然是多个文件,要从工程设定,以及头文件的写法来试一试。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-23 21:43:39 | 显示全部楼层
CodingCat_k 发表于 2020-5-23 21:36
对于多文件编译,首先保证头文件与函数定义文件路径正确;
如果是Windows用户请参考你的IDE如何建立C/C++ ...

嗯,谢谢,应该就是这个原因了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-23 21:57:31 | 显示全部楼层
直接打开DOS,gcc就行了
D:\Tools\Vsd-cpp\test>gcc -o mul_a.exe mul_b.cpp mul_a.cpp
D:\Tools\Vsd-cpp\test>mul_a
hard=2
大概以上定义是没有问题的,只是用的IDE(vsCode),然后编译,执行都在里面,可能他也就提供一个文件编译执行的缺省配置文件,如果是多个大概需要在设定文件中动手,告诉他怎么你想怎么编译,谢谢大家,启发了思路。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-3 01:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表