Author Topic: cmath&cstdlib works fine in main.c file but not in other function.c file  (Read 3574 times)

Offline rhettline

  • Single posting newcomer
  • *
  • Posts: 2
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!

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
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

Tim S.

« Last Edit: June 26, 2020, 04:25:58 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline rhettline

  • Single posting newcomer
  • *
  • Posts: 2
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

Tim S.