简体   繁体   English

在Netbeans中编译C程序,但不使用gcc在cmd中编译

[英]C program compiling in Netbeans, but not compiling in cmd with gcc

I have a C program and have been trying to add http://libmodbus.org/ into my project. 我有一个C程序,一直在尝试将http://libmodbus.org/添加到我的项目中。 I am new to C, but painfully found my way enough to ./configure && make install and everything else needed to create the library. 我是C语言的新手,但痛苦地找到了足够的方法来进行./configure && make install和创建该库所需的所有其他操作。 Now, I have added location of the header files and location of my libmodbus.dll.a file from this link https://forums.netbeans.org/ptopic29782.html 现在,我从此链接https://forums.netbeans.org/ptopic29782.html添加了头文件的位置和libmodbus.dll.a文件的位置。

Now, I compile my program in netbeans, it compiles fine, but when I try to run the program, I receive a undefined reference to modbus_new_rtu . 现在,我在netbeans中编译程序,编译正常,但是当我尝试运行程序时,收到了undefined reference to modbus_new_rtuundefined reference to modbus_new_rtu

When I try to compile the main.c file just itself in cmd like this 当我尝试像这样在cmd中编译main.c文件时

C:\Users\Jensen Home PC\Documents\NetBeansProjects\Modbus_Project>gcc main.c

I recieve 我收到了

main.c:4:20: fatal error: modbus.h: No such file or directory 

If its relevant or helps, this is the path to my header files 如果相关或有帮助,这是我的头文件的路径

C:\MinGW\msys\1.0\local\include\modbus

and my libmodbus.dll.a file, (when I add it in netbeans I only supply the folder C:\\MinGW\\msys\\1.0\\local\\lib because it does not let me select a file but only a folder) 和我的libmodbus.dll.a文件(当我将其添加到netbeans中时,我仅提供文件夹C:\\MinGW\\msys\\1.0\\local\\lib因为它不允许我选择文件,而只能选择一个文件夹)

C:\MinGW\msys\1.0\local\lib\libmodbus.dll.a

So whats the issue here? 那么,这里的问题是什么? I have looked into plenty of Undefined Reference questions on SO but it doesn't seem to explain why I cant compile with gcc. 我已经研究了很多关于SO的未定义参考问题,但似乎并不能解释为什么我不能使用gcc进行编译。 It looks like I am getting an undefined reference because my #include <modbus.h> is not actually including the file in the first place, but for some reason Netbeans thinks I have, so when Netbeans cant find the method, its says undefined. 看来我正在获得未定义的引用,因为我的#include <modbus.h>实际上实际上并未包含该文件,但是由于某种原因Netbeans认为我拥有该文件,因此当Netbeans找不到该方法时,它说未定义。

How do I fix this? 我该如何解决?

Any help is much appreciated. 任何帮助深表感谢。 Thanks! 谢谢! - Dillon -狄龙

There are two separate problems here. 这里有两个单独的问题。

Creating an executable like you're trying to create from source code has two phases - the first one is called compilation, the second one is called linking. 像您要尝试从源代码创建可执行文件一样,创建可执行文件有两个阶段-第一个阶段称为编译,第二个阶段称为链接。 Many times we call the whole process just compilation, but you really compile you have to understand it in a higher resolution. 很多时候,我们将整个过程称为编译,但实际上,您必须以更高的分辨率来理解它。

  • No such file or directory is a compilation error and is due to not mentioning the include directory in the compilation command. No such file or directory是编译错误,并且是由于在编译命令中未提及include目录。 You said yourself that the directory where the missing header file resides in different than the compilation directory. 您自己说过,丢失的头文件所在的目录与编译目录不同。 Use option -I followed by the path to the directory for that. 使用选项-I后跟该目录的路径。 It probably does not happen in Netbeans because IDEs have their own default (but usually configurable) definitions on what directories to look/include files from when they compile your code. 在Netbeans中可能不会发生这种情况,因为IDE在编译代码时对要查找/包含文件的目录具有自己的默认(但通常是可配置的)定义。
  • undefined reference is a linker error. undefined reference是链接器错误。 It happens when in your code you refer to some element (variable/function/struct) that is defined in another source file or library, but the linker doesn't find this definition. 当您在代码中引用在另一个源文件或库中定义的某个元素(变量/函数/结构),但是链接器找不到此定义时,就会发生这种情况。 Your code and this library should link . 您的代码和该库应链接 Use gcc option -l followed by the path to the directory for that. 使用gcc选项-l后跟该目录的路径。

Regarding Your last paragraph in your question - it's important to understand that #include <modbus.h> has nothing to do with undefined reference . 关于问题的最后一段-重要的是要了解#include <modbus.h>undefined reference无关。 Including is checked at compile time . 包括在编译时检查。 Undefined references are found at linking time , which comes only after a successful compilation phase. 链接时会发现未定义的引用, 链接时仅在成功的编译阶段之后才出现。

Read more about compilation and linking to understand better. 阅读有关编译和链接的更多信息以更好地理解。

A nice tutorial about gcc. 关于gcc的不错的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM