A brief introduction to the strings command in Linux _linux

Source: Internet
Author: User
Tags printable characters

In Linux under the software development of friends, few do not know strings command. Let's take a look at man strings first:

 
 

Prints the printable characters in the file. Let me add, this file can be a text file (test.c), executable (test), Dynamic link library (TEST.O), Static link library (TEST.A)

It's not my style to walk away from code without actually verifying it. Let's get some code under the menu. (Code exists in test.c):

#include <stdio.h> 
 
int Add (int x, int y) 
{return 
    x + y; 
} 
 
int main () 
{ 
    int a = 1; 
    int b = 2; 
    int c = Add (A, b); 
    printf ("Oh, my dear, C is%d\n", c); 
 
    return 0; 
} 

Let's look at the results of strings test.c:

[Taoge@localhost learn_c]$ strings test.c  
#include <stdio.h> 
int Add (int x, int y) return 
  x + y; 
int main () 
  int a = 1; 
  int b = 2; 
  int c = Add (A, b); 
  printf ("Oh, my dear, C is%d\n", c); 
  return 0; 
[Taoge@localhost learn_c]$  

As you can see, you do print out a lot of characters in test.c.

Below, we try the executable file with strings, as follows:

[Taoge@localhost learn_c]$ gcc test.c  
[taoge@localhost learn_c]$ strings a.out/lib/ld-linux.so.2 =$ 
TsU 
__gmon_start__ 
libc.so.6 
_io_stdin_used 
printf 
__libc_start_main 
glibc_2.0 
PTRH  
[^_] 
Oh, my dear, C is%d 
[Taoge@localhost learn_c]$  

As you can see, many characters are printed out of the a.out.

In fact, if you have a target file, a static library, or a dynamic library, you can also print with the strings command. Let's take a look at:

Xxx.h file:

 
 

XXX.C file:

#include <stdio.h> 
#include "xxx.h" 
 
void print () 
{ 
  printf ("Rainy days\n"); 
} 

Then, let's look at how to make a static, dynamic library (which will continue to be detailed in subsequent posts):

[Taoge@localhost learn_strings]$ ls xxx.c xxx.h [taoge@localhost learn_strings]$ gcc-c xxx.c [Taoge@localhost learn_s trings]$ ar rcs libxxx.a xxx.o [taoge@localhost learn_strings]$ gcc-shared-fpic-o libxxx.so xxx.o [taoge@localhost le arn_strings]$ ls libxxx.a libxxx.so xxx.c xxx.h xxx.o [taoge@localhost learn_strings]$ strings XXX.O-Rainy days [taoge @localhost learn_strings]$ strings libxxx.a!<arch>/1437887339 0 0 0 ' rprint xxx.o/1  
437887333 501 502 100664 848 ' Rainy days GCC: (GNU) 4.4.4 20100726 (Red Hat 4.4.4-13). Symtab. Strtab . Rel.text. Data. BSS. rodata. Comment. Note. Gnu-stack xxx.c print puts [Taoge@localhost learn_strings]$ [Taoge@localhost learn_strings]$] [Taoge@localhost Lear 
n_strings]$ strings libxxx.so __gmon_start__ _init _fini __cxa_finalize _jv_registerclasses print puts libc.so.6 _edata __bss_start _end glibc_2.1.3 glibc_2.0 rainy days [Taoge@localhost learn_strings]$  
 

See it.

The strings command is simple, looks like nothing, but actually has a lot of uses.  Now, let me give you an example. In large software development, assuming that there are 100. c/.cpp files, this. cpp file eventually generates 10. So libraries, how can you quickly tell if a. c/.cpp file has been compiled into that. So library? Of course, you may have to say, look at makefile don't know. Yes, look makefile certainly can, but the following method is better, directly with the command:

Strings-f "*.so" | grep "XXXXXX"

If you do not understand, then take the above applet as an example to illustrate, but here we consider all the files, as follows:

[Taoge@localhost learn_c]$ Strings-f * | grep ' My dear ' 
A.out:oh, my dear, C are%d 
test.c:   printf ("Oh, my dear, C is%d\n", c); 
[Taoge@localhost learn_c]$  

You can see that the source file test.c and executable files are "my dear" string, all of a sudden found the corresponding file, clear it. If a. c/.cpp file is compiled into the. So library, then strings-f * | grep "My dear" must be able to find the corresponding. so file where "my dear" is a log string in the. c/.cpp file (for example, print in printf).

The role of strings first introduced to this, is a kind of introduction to familiar with the strings bar.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.