Unique shared library issues
Problem description
Recently, when I tried to link a self-built C language shared library to a local project, I encountered a link error and prompted "Undefined reference". The error message is as follows:
<code>/bin/ld: /tmp/cchb7mj8.o: in function `sdl_main': main.c:(.text 0x3c): undefined reference to `sdl_enterappmaincallbacks' ... (其他类似的未定义引用) ... collect2: error: ld returned 1 exit status make: *** [makefile:7: all] error 1</code>
Troubleshooting process
Trying to recompile the library multiple times and trying different methods, none of them worked. After searching for relevant information on search engines, I found that someone encountered similar problems in a forum, but was using a 32-bit toolchain, while my toolchain was 64-bit. After excluding toolchain version differences, consider trying different compilers.
Solution
The problem was solved after initially compiling using GCC and switching to Clang.
Problem recurrence and analysis
To understand the root cause of the problem, a simple test project was created:
- lib.h:
<code class="c">#pragma once int add(int a, int b);</code>
- lib.c:
<code class="c">#include "lib.h" int add(int a, int b) { return ab; }</code>
- main.c:
<code class="c">#include "lib.h" #include <stdio.h> int main () { printf("4 3=%d\n", add(4, 3)); return 0; }</stdio.h></code>
- build_so.sh (Clang compiles shared library):
<code class="bash">clang -std=c11 -c -o lib.o lib.c clang -shared -fpic -o libm.so lib.o</code>
- build_main.sh (GCC compiles the main program, links to the shared library):
<code class="bash">gcc -std=c11 -L. -l:libm.so main.c -o main</code>
Using the above script, the link failed with a similar "Undefined Reference" error:
<code>/bin/ld: /tmp/ccymm8ki.o: in function `main': main.c:(.text 0x13): undefined reference to `add' collect2: error: ld returned 1 exit status</code>
However, if the compilers in build_so.sh
and build_main.sh
are interchangeable (i.e. using GCC to compile the shared library and Clang compiles the main program), the link is successful.
in conclusion
This issue may be related to the differences in internal symbol handling between compilers when generating and linking shared libraries. When building shared libraries and executables with different compilers, you need to ensure that they are compatible. While this problem can be avoided in most cases using the same compiler, this example illustrates potential compatibility issues between different compilers. If you need to use different compilers in your project, you need to double-check and make sure that symbolic parsing is done correctly between them.
The above is the detailed content of Unique shared library issues. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



SQLLIMIT clause: Control the number of rows in query results. The LIMIT clause in SQL is used to limit the number of rows returned by the query. This is very useful when processing large data sets, paginated displays and test data, and can effectively improve query efficiency. Basic syntax of syntax: SELECTcolumn1,column2,...FROMtable_nameLIMITnumber_of_rows;number_of_rows: Specify the number of rows returned. Syntax with offset: SELECTcolumn1,column2,...FROMtable_nameLIMIToffset,number_of_rows;offset: Skip

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Detailed explanation of the SQLORDERBY clause: The efficient sorting of data ORDERBY clause is a key statement in SQL used to sort query result sets. It can be arranged in ascending order (ASC) or descending order (DESC) in single columns or multiple columns, significantly improving data readability and analysis efficiency. ORDERBY syntax SELECTcolumn1,column2,...FROMtable_nameORDERBYcolumn_name[ASC|DESC];column_name: Sort by column. ASC: Ascending order sort (default). DESC: Sort in descending order. ORDERBY main features: Multi-column sorting: supports multiple column sorting, and the order of columns determines the priority of sorting. since

Common errors and solutions when connecting to databases: Username or password (Error 1045) Firewall blocks connection (Error 2003) Connection timeout (Error 10060) Unable to use socket connection (Error 1042) SSL connection error (Error 10055) Too many connection attempts result in the host being blocked (Error 1129) Database does not exist (Error 1049) No permission to connect to database (Error 1000)

The SQL INSERT statement is used to add new rows to a database table, and its syntax is: INSERT INTO table_name (column1, column2, ..., columnN) VALUES (value1, value2, ..., valueN);. This statement supports inserting multiple values and allows NULL values to be inserted into columns, but it is necessary to ensure that the inserted values are compatible with the column's data type to avoid violating uniqueness constraints.

Reasons for Navicat connection timeout: network instability, busy database, firewall blocking, server configuration problems, and improper Navicat settings. Solution steps: Check network connection, database status, firewall settings, adjust server configuration, check Navicat settings, restart the software and server, and contact the administrator for help.

Add new columns to an existing table in SQL by using the ALTER TABLE statement. The specific steps include: determining the table name and column information, writing ALTER TABLE statements, and executing statements. For example, add an email column to the Customers table (VARCHAR(50)): ALTER TABLE Customers ADD email VARCHAR(50);

Navicat for MongoDB cannot view the database password because the password is encrypted and only holds connection information. Retrieving passwords requires MongoDB itself, and the specific operation depends on the deployment method. Security first, develop good password habits, and never try to obtain passwords from third-party tools to avoid security risks.
