程序设计基础:看书笔记:第一课_打印字符串

首先介绍vs studio的安装,安装完成后,我们在vs studio中新建一个C++空项目

右键添加新建项

新建一个2.1.cpp文件

我们首先来打印字符串

在2.1.cpp文件中输入:

snippet.cpp
#include <iostream>   
int main()            
{    
    std::cout << "good luck" << std::endl;
    return 0;         
}                     

点击运行

可以看到弹出一个黑框框

这里就进行完毕了,接下来我们对代码进行研究

#include 表示去系统标准库的 include 目录找,通常用于标准库头文件。

在两个位置可以看见它,首先是C:\Program Files\Microsoft Visual Studio\<版本>\VC\Tools\MSVC\<版本号>\include\iostream,假设你是默认安装的,

点击进去看看内容是什么

#include <iostream>,就是表示将这个iostream文件的内容复制到当前位置

这个文件里面是一些头文件的声明

那么

问题1: 如果不准写`#include <iostream>`,要你手动代替这个功能,怎么实现

尝试1:直接把这个文件复制过来

snippet.cpp
// iostream standard header
 
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
#ifndef _IOSTREAM_
#define _IOSTREAM_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <istream>
#include <ostream>
 
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
#ifdef _M_CEE_PURE
__PURE_APPDOMAIN_GLOBAL extern istream cin;
__PURE_APPDOMAIN_GLOBAL extern ostream cout;
__PURE_APPDOMAIN_GLOBAL extern ostream cerr;
__PURE_APPDOMAIN_GLOBAL extern ostream clog;
__PURE_APPDOMAIN_GLOBAL extern istream* _Ptr_cin;
__PURE_APPDOMAIN_GLOBAL extern ostream* _Ptr_cout;
__PURE_APPDOMAIN_GLOBAL extern ostream* _Ptr_cerr;
__PURE_APPDOMAIN_GLOBAL extern ostream* _Ptr_clog;
 
__PURE_APPDOMAIN_GLOBAL extern wistream wcin;
__PURE_APPDOMAIN_GLOBAL extern wostream wcout;
__PURE_APPDOMAIN_GLOBAL extern wostream wcerr;
__PURE_APPDOMAIN_GLOBAL extern wostream wclog;
__PURE_APPDOMAIN_GLOBAL extern wistream* _Ptr_wcin;
__PURE_APPDOMAIN_GLOBAL extern wostream* _Ptr_wcout;
__PURE_APPDOMAIN_GLOBAL extern wostream* _Ptr_wcerr;
__PURE_APPDOMAIN_GLOBAL extern wostream* _Ptr_wclog;
#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT istream cin;
_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT ostream cout;
_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT ostream cerr;
_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT ostream clog;
extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT istream* _Ptr_cin;
extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT ostream* _Ptr_cout;
extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT ostream* _Ptr_cerr;
extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT ostream* _Ptr_clog;
 
_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT wistream wcin;
_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT wostream wcout;
_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT wostream wcerr;
_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT wostream wclog;
extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT wistream* _Ptr_wcin;
extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT wostream* _Ptr_wcout;
extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT wostream* _Ptr_wcerr;
extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT wostream* _Ptr_wclog;
 
#ifdef _CRTBLD // TRANSITION, ABI: _Winit appears to be unused
class _CRTIMP2_PURE_IMPORT _Winit {
public:
    __thiscall _Winit();
    __thiscall ~_Winit() noexcept;
 
private:
    __PURE_APPDOMAIN_GLOBAL static int _Init_cnt;
};
#endif // defined(_CRTBLD)
 
#endif // ^^^ !defined(_M_CEE_PURE) ^^^
_STD_END
#pragma pop_macro("new")
_STL_RESTORE_CLANG_WARNINGS
#pragma warning(pop)
#pragma pack(pop)
#endif // _STL_COMPILER_PREPROCESSOR
#endif // _IOSTREAM_
 
int main()
{
    std::cout << "good luck" << std::endl;
    return 0;
}

尝试编译,嘿嘿,成功了,证实了#include <iostream>就是把iostream文件的内容复制过来

尝试2,删除不必要的内容

使用眼球技术

snippet.cpp
#include <ostream>
_STD_BEGIN
_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT ostream cout;
_STD_END
int main()
{
    std::cout << "good luck" << std::endl;
    return 0;
}

其中#include<ostream>还是引入了一个头文件

而_STD_BEGIN、_STD_END,是STD命名空间的声明

_EXPORT_STD extern "C++" __PURE_APPDOMAIN_GLOBAL _CRTDATA2_IMPORT ostream cout;的意思是namespace std 里面,有一个 ostream 类型的、名叫 cout 的对象;它是在别处(DLL/库)里真正创建的,我们这里只是声明它存在;请用 C++ 的链接规则、从库中导入它。

那我们再打开ostream文件,里面按ctrl+F 没有cout 的定义,这里我们问deepseek,说ostream类定义在MSVC的程序文件里。

但是不应该在此处死心,我们应该秉承就是玩的态度,看看能不能反编译。

下载一个ghidra,deepseek说我们大概率得不到结果,但是还是准备试试,2026年09月7日23:16。

搜索cout,找到了上面程序的汇编代码,可以清楚的找到good luck,这提示我们字符串编码还是要小心。

__CheckForDebuggerJustMyCode(&DAT_140023074);
  this = (basic_ostream<> *)thunk_FUN_1400118c0((longlong *)cout_exref,"good luck");
  std:basic_ostream<>::operator<<
            (this,(_func_basic_ostream<>_ptr_basic_ostream<>_ptr *)&LAB_14001103c);
  return 0;
}

然后追查到了C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Redist\MSVC\14.44.35112\onecore\debug_nonredist\x64\Microsoft.VC143.DebugCRT\msvcp140d.dll

找到了点内容

                             **************************************************************
                             * class std::basic_ostream<char,struct std::char_traits<c... *
                             **************************************************************
                             0xd1fc0  701  ?cout@std@@3V?$basic_ostream@DU?$char_traits@D@
                             Ordinal_701                                     XREF[7]:     Entry Point(*), 
                             ?cout@std@@3V?$basic_ostream@DU?$char_traits@D               FUN_1800012c0:1800012d4(*), 
                             std::cout                                                    FUN_180014390:180014399(*), 
                                                                                          FUN_180014390:1800143d1(*), 
                                                                                          FUN_180014390:18001440d(*), 
                                                                                          FUN_180094c00:180094c04(*), 
                                                                                          1800b16f8(*)  
       1800d1fc0 00              basic_os   <Empty-Structure>

到这里就无功而返了,2026年09月08日21:44。

https://github.com/microsoft/STL有该工具的开源代码。

在stl/src/cout.cpp路径下定义了cout

snippet.cpp
#include "init_locks.hpp"
 
#pragma warning(disable : 4074)
#pragma init_seg(compiler)
static std::_Init_locks initlocks;
 
_STD_BEGIN
 
__PURE_APPDOMAIN_GLOBAL static filebuf fout(stdout);
 
#if defined(_M_CEE_PURE)
__PURE_APPDOMAIN_GLOBAL extern ostream cout(&fout);
#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT ostream cout(&fout);
#endif // ^^^ !defined(_M_CEE_PURE) ^^^
 
struct _Init_cout { // ensures that cout is initialized
    __CLR_OR_THIS_CALL _Init_cout() { // initialize cout
        _Ptr_cout = &cout;
        if (_Ptr_cin != nullptr) {
            _Ptr_cin->tie(_Ptr_cout);
        }
 
        if (_Ptr_cerr != nullptr) {
            _Ptr_cerr->tie(_Ptr_cout);
        }
    }
};
 
__PURE_APPDOMAIN_GLOBAL static _Init_cout init_cout;
 
_STD_END

其中 ostream cout(&fout) 定义一个 ostream 类型的对象,起名叫 cout,用 &fout 作为参数调用它的构造函数来初始化它。

到这里,我们会发现cout其实是一个对象,并不是一个函数,这个就是它和printf 的区别。

怪不得是用« 往里面塞东西。20260914-13:57

snippet.c
int puts(const char *);
 
int main(void) {
    puts("good luck");
    return 0;
}

最后用C语言搞定,有点擦边但是不管了。

该主题尚不存在

您访问的页面并不存在。如果允许,您可以使用创建该页面按钮来创建它。

  • 程序设计基础/看书笔记/第一课_打印字符串.txt
  • 最后更改: 2026/09/15 05:16
  • 由 44.220.2.97