简体   繁体   中英

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. I am new to C, but painfully found my way enough to ./configure && make install and everything else needed to create the library. 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

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 .

When I try to compile the main.c file just itself in cmd like this

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)

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. 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.

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. 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. 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.
  • undefined reference is a linker error. 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.

Regarding Your last paragraph in your question - it's important to understand that #include <modbus.h> has nothing to do with 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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