Code::Blocks

User forums => Help => Topic started by: rhettline on June 26, 2020, 04:11:14 pm

Title: cmath&cstdlib works fine in main.c file but not in other function.c file
Post by: rhettline on June 26, 2020, 04:11:14 pm
I met a very weird situation with my c++ project. When I include standard libraries such as "#include <cmath>" and "#include <cstdlib>" in main.c file, it can be successfully built and run. However, if I want to call a function file including these libraries, for example random.c, I got the error like "fatal error: cstdlib: No such file or directory".

Does anybody have any idea about that? I don't think the standard library need to be linked. I am using GNU GCC compiler with default setup.
C compiler: gcc.exe
C++ compiler: g++.exe
Linker for dynamic libs: g++.exe
Linker for static libs: ar.exe
Debugger: GDB/CDB debugger: Default
Resource compiler: windres.exe
Make program: mingw32-make.exe

Many thanks!
Title: Re: cmath&cstdlib works fine in main.c file but not in other function.c file
Post by: stahta01 on June 26, 2020, 04:23:15 pm
Check the file extension!
C++ should have an cpp file extension while C has an c file extension!

NOTE: You are likely using main.cpp because main.c would not work!!!
Edit2: main.C works on Linux as C++.

Edit: Build a full build log if you can not find and fix the problem.
http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F (http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F)

Tim S.

Title: Re: cmath&cstdlib works fine in main.c file but not in other function.c file
Post by: rhettline on June 26, 2020, 05:14:05 pm
Thanks a lot Tim! You are absolutely right. I am so stupid to check all the linker issues, but the fundamental reason is .cpp

Check the file extension!
C++ should have an cpp file extension while C has an c file extension!

NOTE: You are likely using main.cpp because main.c would not work!!!
Edit2: main.C works on Linux as C++.

Edit: Build a full build log if you can not find and fix the problem.
http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F (http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F)

Tim S.