我已经在netbeans上安装了mingw用于C编程.
然后我创建了一个文件"Myfile.c",我在其中创建了一个方法callme().
但是这个文件不包含main方法.
我想callme()从另一个包含main方法的文件中调用.
myfile.c文件
#include <stdio.h>
void callme() {
printf("I am called");
}
Run Code Online (Sandbox Code Playgroud)
EntryFile.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
// How to call callme() from here OR use this method anyhow.
return (EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
出于某些原因,我不想创建主方法Myfile.c.
编辑
myfile.c文件
#include <stdio.h>
void callme();
void callme() {
printf("I am called");
}
Run Code Online (Sandbox Code Playgroud)
EntryFile.c
#include <stdio.h>
int main(int argc, char** argv) {
callme();
}
Run Code Online (Sandbox Code Playgroud)
错误
"/C/MinGW/MSYS/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/Afzalex/Documents/NetBeansProjects/CppStoreRoom'
"/C/MinGW/MSYS/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/cppstoreroom.exe
make.exe[2]: Entering directory `/c/Users/Afzalex/Documents/NetBeansProjects/CppStoreRoom'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/Myfile.o.d"
gcc -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/Myfile.o.d" -o build/Debug/MinGW-Windows/Myfile.o Myfile.c
make.exe[2]: *** No rule to make target `newmain.cpp', needed by `build/Debug/MinGW-Windows/newmain.o'. Stop.
make.exe[2]: Leaving directory `/c/Users/Afzalex/Documents/NetBeansProjects/CppStoreRoom'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/c/Users/Afzalex/Documents/NetBeansProjects/CppStoreRoom'
make.exe": *** [.build-impl] Error 2
Run Code Online (Sandbox Code Playgroud)
正如@LPs在他的回答中指出的那样,你应该创建一个MyFile.h包含内容的文件
/* include guards (research the term if you don't know it) */
#ifndef MYFILE_H
#define MYFILE_H
void callme(void);
#endif // MYFILE_H
Run Code Online (Sandbox Code Playgroud)
并包含您EntryFile.c使用的此文件
#include "MyFile.h"
Run Code Online (Sandbox Code Playgroud)
或者,您可以直接将原型编写成EntryFile.c使用
extern void callme(void);
Run Code Online (Sandbox Code Playgroud)
在使用该函数之前的全局范围.两种方式都会使编译器知道该函数,您可以在main函数中使用它.