-2

I wanted to use NODEFAULTLIB linker option in my project. However, when I try to use CRT-based functions like printf and ... Visual studio produces the following errors:

ERROR: unresolved external symbol printf.

So I try to use the following macro:

#pragma comment(lib, "msvcrt.lib")

but it doesn't solve the problem. I get the same error again. However, how can I solve this issue?

  • 3
    Why do you use NODEFAULTLIB if you want default lib functions? – Simon Mourier Oct 22 '22 at 11:13
  • I wanted just to experience different approaches to build a project. – user19245401 Oct 22 '22 at 11:14
  • 2
    Well NODEFAULTLIB means exactly what its name suggest, it removes default library so you'll get tons of unresolved external errors unless you reimplements them yourselve. – Simon Mourier Oct 22 '22 at 11:16
  • Soo how can I inform compiler or linker which I wanna use printf function and its library? is there any solution for this thing? – user19245401 Oct 22 '22 at 11:18
  • Not sure I understand what you mean by that. Compiler ands linker are two different things. Lib is about linking. If you use NODEFAULTLIB, you can't link to any method implemented in these lib(s) https://stackoverflow.com/a/29429820/403671 – Simon Mourier Oct 22 '22 at 11:24
  • 1
    add *msvcrt.lib* direct to linker input libs, but no via #pragma comment – RbMm Oct 22 '22 at 11:25
  • Calling `printf` without the CRT does work but it is probably undefined behavior because you don't initialize the CRT correctly. – Anders Oct 22 '22 at 12:27
  • *"I wanted just to experience different approaches to build a project."* - That doesn't make any sense. Trying to build a project by making sure that required dependencies will not be available isn't going to be a successful build. It's unclear what problem you are *really* trying to solve. This is probably just another example of the [XY Problem](https://xyproblem.info). – IInspectable Oct 22 '22 at 13:24

1 Answers1

0

/NODEFAULTLIB removes all default libraries from the list of libraries it searches when resolving external references. And according to CRT library features, you need to link against the Microsoft C runtime library .lib files.
As far as Multi-threaded Debug DLL (/MDd) concerned, Linking ucrtd.lib, vcruntimed.lib and msvcrtd.lib at Configuration Properties > Linker > Input > Additional Dependencies works for me.

YangXiaoPo-MSFT
  • 1,589
  • 1
  • 4
  • 22