1. 首页
  2. 编程语言
  3. C
  4. 21 st Century C 2nd Edition O'reilly

21 st Century C 2nd Edition O'reilly

上传者: 2020-07-17 06:54:50上传 PDF文件 5.69MB 热度 18次
SECOND EDITION 21st Century C Ben klemens Beng. Cambridge. Farnham·Kn· Sebastopol, Tokyo OREILLY° 21st Century C, Second Edition by ben klemens Copyright O 2015 Ben Klemens. All rights reserved Printed in the united states of america Published by o reilly media, InC, 1005 Gravenstein Highway North, Sebastopol, CA95472 OReilly books may be purchased for educational, business, or sales promotional use Online editions are alsoavailableformosttitles(http://www.safaribooksonline.com).Formoreinformationcontactourcor porate/institutionalsalesdepartment800-998-9938orcorporate@oreilly.com Editors: Rachel Roumeliotis and Allyson MacDonald Indexer: Judy McConville Production Editor: Nicole Shelby Interior Designer: David Futato Copyeditor: Becca Freed Cover Designer: Karen Montgomery Proofreader: Amanda Kersey Illustrator: rebecca demarest eptember 2014: Second edition Revision history for the second edition 2014-09-24: First Release Seehttp://oreilly.com/catalog/errata.csp?isbn=9781491903896forreleasedetails The O Reilly logo is a registered trademark of O Reilly Media, InC. 21st Century C, the cover image, and elated trade dress are trademarks of o reilly media, inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O Reilly Media, Inc. was aware of a trade- mark claim, the designations have been printed in caps or initial caps While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. USe of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complie h such lice 978-1-491-90389-6 [LSII Table of contents Prefab Part the environment 1. Set Yourself Up for Easy Compilation............. Use a Package Manager Compiling c with Windows POSIX for Windows Compiling c with POSIX Compiling C Without POSIX 244678 Which Way to the library? A Few of My Favorite Flags 10 Paths Runtime Linking 14 Using makefiles 15 Setting variables The rules 18 Using Libraries from Source 22 Using Libraries from Source(even if Your Sysadmin Doesnt Want You To) 23 Compiling c programs via Here document 25 Include header Files from the Command Line 25 The Unified Header 26 Here documents 27 Compiling from stdin 28 2. Debug, Test, Document. 31 USing a Debugger 31 A Debugging Detective Story 34 GDB Variables Print your structures Using valgrind to Check for Errors 48 Unit Testing 50 Using a program as a library 53 Coverage 54 Error Checking 55 What is the User's involvement in the error? 56 The Context in Which the User is Working 57 How Should the error indication be returned? 59 Interweaving documentation 59 OXygen Literate Code with CWeB 61 3. Packaging Your Project........................... 65 The shell 66 Replacing shell Commands with Their Outputs Use the Shells for loops to operate on a set of files 68 Test for files 70 fc Makefiles Vs. Shell Scripts 75 Packaging Your Code with Autotools An autotools demo 79 Describing the Makefile with Makefile.am 82 The configure Script 87 4. Version〔 ontrol 93 Changes via diff 94 Gits Objects The Stash Trees and Their Branches 100 erging 101 The rebase 103 Remote repositories 104 5. Playing Nice with Others Dynamic Loading 107 The Limits of Dynamic Loading 110 The process 110 Writing to Be read by nonnatives 111 The Wrapper Function 111 iv Table of Contents Smuggling Data Structures Across the Border 112 Linki g 114 Python Host 114 Compiling and linking 116 The Conditional Subdirectory for Automake Distutils backed with autotools 118 Part Il. The Language 6. Your pal the pointer Automatic, Static, and Manual Memory 123 Persistent State Variables 128 Pointers without malloc 129 Structures Get Copied, Arrays Get Aliased 131 malloc and Memory-Twiddling 134 The Fault is in our star All the pointer arithmetic You need to Know 136 Typedef as a teaching tool 139 7. Inessential C Syntax that Textbooks Spend a Lot of Time Covering. 141 Dont bother explicitly returning from main 141 Let declarations flow 142 Set array Size at runtime 144 Cast Less 145 Enums and strings 147 Labels, gotos, switches, and breaks 148 goto Considered 149 152 te float 153 Comparing Unsigned Integers 156 Safely Parse Strings to Numbers 156 8. Important C Syntax that Textbooks Often Do Not Cover. ............ 161 Cultivate robust and Flourishing macros 161 The Preprocessor 166 Test macros 170 Header guards 172 Linkage with static and extern Externally Linked Variables in Header Files 175 The const Keyword 177 Noun-Adjective Form 178 Table of Contents I Tension 179 Depth 179 The char const * issue 180 9. Easier Text Handling................ 185 Making String Handling Less Painful with asprintf 185 Security 88 Constant Strings 189 Extending Strings with asprintf 191 A paean to strtok 192 Unicode 197 The Encoding for C Code 199 Unicode libraries 200 The Sample code 201 10. Better structures ,205 Co pound d literals 206 Initialization via Compound literals 207 Variadic macros 208 Safely Terminated Lists 209 Multiple lists 210 F 212 Vectorize a function 212 Designated initializers 214 Initialize arrays and Structs with Zeros 216 Typedefs save the day 217 a Style note 218 Return Multiple items from a function 220 Reporting errors 221 Flexible Function Inputs 223 Declare Your Function as printf-Style 224 Optional and Named Arguments 226 Polishing a dull function 228 The Void pointer and the structures it points to 234 Functions with Generic Inputs 234 Generic structures 239 11. Object-Oriented Programming in C. ,245 Extending Structures and Dictionaries 247 Implementing a Dictionary 249 C with fewer seams 253 Functions in your structs 258 Table of contents Vtables 262 267 Private struct elements 268 Overload 270 Generic 271 Count references 274 Example: A Substring object 275 Example: An Agent-Based Model of Group Formation 279 Conclusion 286 12. Parallel threads The Environment 290 The Ingredients 291 OpenmP 292 Compiling OpenMP, pthreads, and C atoms 294 Interference 295 Map-reduce 296 Multiple tasks 297 Thread local 298 ocalizing Nonstatic Variables 300 Shared resources 300 Atoms 305 Pthreads 308 C atoms 312 Atomic structs 315 13. Libraries 321 GLib 321 POSIX 322 Parsing regular expressions 322 USing mmap for Gigantic Data Sets 327 The GNU Scientific Library 330 SQLite 332 The Queries 334 libxml and cURL 335 Epilogue. 341 A.C10 Table of contents glossary ,,363 References ,,,,,,367 Index 371 I Table of Contents
用户评论