0

I would like to use the libcpuid library and provide it as a bundle to my main application written in Python (I'm planning to wrap the C library using ctypes).

I am trying to run the example program available in the official documentation but I am always getting the error:

main.c:2:10: fatal error: libcpuid.h: No such file or directory
 #include <libcpuid.h>
          ^~~~~~~~~~~~

What I did was following the instruction from the Readme.md of the project:

git clone https://github.com/anrieff/libcpuid.git
cd libcpuid/
libtoolize
autoreconf --install
./configure
make

The compilation went through without errors and I noticed that I had a new hidden folder libcpuid/.libs/ containing the following files:

asm-bits.o                           
cpuid_main.o                         
libcpuid.a                           
libcpuid.la -> ../libcpuid.la        
libcpuid.lai                         
libcpuid.so -> libcpuid.so.14.0.0*   
libcpuid.so.14 -> libcpuid.so.14.0.0*
libcpuid.so.14.0.0*                  
libcpuid_util.o                      
libcpuid.ver                         
msrdriver.o                          
rdmsr.o                              
rdtsc.o                              
recog_amd.o                          
recog_intel.o 

I know that adding this library to the system shared libraries will probably fix all my issues, but I am trying to see if I can deploy this library together with my app so that I can control the version that is used.

I have created a new main.c file outside the project directory containing the sample code:

#include <stdio.h>
#include <libcpuid.h>


int main(void)
{
    if (!cpuid_present()) {                                                // check for CPUID presence
        printf("Sorry, your CPU doesn't support CPUID!\n");
        return -1;
    }


    struct cpu_raw_data_t raw;                                             // contains only raw data
    struct cpu_id_t data;                                                  // contains recognized CPU features data


    if (cpuid_get_raw_data(&raw) < 0) {                                    // obtain the raw CPUID data
        printf("Sorry, cannot get the CPUID raw data.\n");
        printf("Error: %s\n", cpuid_error());                          // cpuid_error() gives the last error description
        return -2;
    }


    if (cpu_identify(&raw, &data) < 0) {                                   // identify the CPU, using the given raw data.
        printf("Sorrry, CPU identification failed.\n");
        printf("Error: %s\n", cpuid_error());
        return -3;
    }

    printf("Found: %s CPU\n", data.vendor_str);                            // print out the vendor string (e.g. `GenuineIntel')
    printf("Processor model is `%s'\n", data.cpu_codename);                // print out the CPU code name (e.g. `Pentium 4 (Northwood)')
    printf("The full brand string is `%s'\n", data.brand_str);             // print out the CPU brand string
    printf("The processor has %dK L1 cache and %dK L2 cache\n",
            data.l1_data_cache, data.l2_cache);                            // print out cache size information
    printf("The processor has %d cores and %d logical processors\n",
            data.num_cores, data.num_logical_cpus);                        // print out CPU cores information


    printf("Supported multimedia instruction sets:\n");
    printf("  MMX         : %s\n", data.flags[CPU_FEATURE_MMX] ? "present" : "absent");
    printf("  MMX-extended: %s\n", data.flags[CPU_FEATURE_MMXEXT] ? "present" : "absent");
    printf("  SSE         : %s\n", data.flags[CPU_FEATURE_SSE] ? "present" : "absent");
    printf("  SSE2        : %s\n", data.flags[CPU_FEATURE_SSE2] ? "present" : "absent");
    printf("  3DNow!      : %s\n", data.flags[CPU_FEATURE_3DNOW] ? "present" : "absent");


    printf("CPU clock is: %d MHz (according to your OS)\n",
            cpu_clock_by_os());                                            // print out the CPU clock, according to the OS
    printf("CPU clock is: %d MHz (tested)\n", cpu_clock_measure(200, 0));  // print out the CPU clock, measured with RDTSC.


    return 0;
}

Then I tried to follow several instructions that I found online on how to build a C program using a shared library:

gcc -l/home/user/temp/libcpuid/libcpuid/.libs/libcpuid.so main.c
gcc -Wall -o test main.c -lcpuid
gcc -L/home/user/temp/libcpuid/libcpuid/.libs/ -Wall -o test main.c -lcpuid
export LD_LIBRARY_PATH=/home/user/temp/libcpuid/libcpuid/.libs/
gcc -o main main.c -lcpuid -L/home/user/temp/libcpuid/libcpuid/.libs/

but with every command I only and always got the same error:

main.c:2:10: fatal error: libcpuid.h: No such file or directory
 #include <libcpuid.h>
          ^~~~~~~~~~~~

I tried also to use double quotes instead of angular brackets for the libcpuid.h but there was no difference.

I am sure I am doing something silly since I have a very limited experience with C (perhaps the make command is not building a shared library? How can I check?)

What I have to do to run my sample app without having to add this library to the system shared libraries?

Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69
  • It probably is the makefile being setup correctly. Do you have the relevant code? – Alerra Aug 18 '18 at 23:06
  • This is the content of the Makefile of the library: https://gist.github.com/leinardi/3688f4e365f8401a858bd230051d0f7c – Roberto Leinardi Aug 18 '18 at 23:11
  • Well you didn't include the repository of ... include files... so... what did you expect ? – Stargateur Aug 18 '18 at 23:13
  • 2
    you executed make, but usually the headers are only copied into a path that gcc can find by default if you use `make install` afterwards. Otherwise you can add searchpaths for include directories with the `-I` (caputal i) flag for gcc – PeterT Aug 18 '18 at 23:14
  • @Stargateur I am sorry but, as I said I have very limited experience with C: could you please elaborate? – Roberto Leinardi Aug 18 '18 at 23:16
  • if you want more options you can take a look at [this question](https://stackoverflow.com/questions/558803/how-to-add-a-default-include-path-for-gcc-in-linux) – PeterT Aug 18 '18 at 23:18
  • @PeterT hey thanks! the `-I` option worked! `gcc -o main main.c -lcpuid -L/home/user/temp/libcpuid/libcpuid/.libs/ -I/home/user/temp/libcpuid/libcpuid`. If you want you can officially answer the question so I can mark it as solved. Thanks again. – Roberto Leinardi Aug 18 '18 at 23:21
  • Possible duplicate of [How to add a default include path for GCC in Linux?](https://stackoverflow.com/questions/558803/how-to-add-a-default-include-path-for-gcc-in-linux) – PeterT Aug 18 '18 at 23:27
  • I feel like marking it as a duplicate of the question I linked might be more appropriate, since all the possibilities can then be concentrated at one place instead of scattering it across a bunch of questions. – PeterT Aug 18 '18 at 23:29
  • Have you run `make install`? Running just `make` only prepares the libraries etc for installation — you still need to actually install it. Since you didn't configure it with a `--prefix=/opt/cpuid` or similar option, it will probably be installed under `/usr/local` (headers in `/usr/local/include`, libraries in `/usr/local/lib`, etc). You most probably won't need to add those locations to the compiler command line when you use the library after installing it. – Jonathan Leffler Aug 18 '18 at 23:34
  • Hi @JonathanLeffler I would like to ship the library bundled with my app, in a "portable", so that the user doesn't have to force the user to stick to the version of the lib I require. – Roberto Leinardi Aug 19 '18 at 09:38
  • @PeterT I still think this question could be useful to very C beginners like me because the one you linked can be found only if you know already what the problem is. – Roberto Leinardi Aug 19 '18 at 09:40
  • 1
    The easiest way to do that is to build and link a static library rather than a shared library and then link the static library into your program. What your program needs is then part of the executable which avoids a number of problems. If you decide to ship the shared object, you have a variety of installation and configuration problems to resolve — where to put the library, how to deal with other (older, newer) versions of the library already installed, how to ensure it is picked up at runtime, even if the program is run by root, etc. – Jonathan Leffler Aug 19 '18 at 15:50
  • @JonathanLeffler Yeah, I realized that too. For example with the dynamic link I was not able to run my test app as root, but with the static link worked fine. Thanks. – Roberto Leinardi Aug 19 '18 at 18:21

0 Answers0