Home Backend Development C++ Unique shared library issues

Unique shared library issues

Apr 03, 2025 pm 08:00 PM
c language ai red 2025

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>
Copy after login

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>
Copy after login
  • lib.c:
 <code class="c">#include "lib.h" int add(int a, int b) { return ab; }</code>
Copy after login
  • 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>
Copy after login
  • 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>
Copy after login
  • 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>
Copy after login

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>
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Master SQL LIMIT clause: Control the number of rows in a query Master SQL LIMIT clause: Control the number of rows in a query Apr 08, 2025 pm 07:00 PM

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

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

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).

Master the ORDER BY clause in SQL: Effectively sort data Master the ORDER BY clause in SQL: Effectively sort data Apr 08, 2025 pm 07:03 PM

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

Navicat connects to database error code and solution Navicat connects to database error code and solution Apr 08, 2025 pm 11:06 PM

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)

How to write the latest tutorial on SQL insertion statement How to write the latest tutorial on SQL insertion statement Apr 09, 2025 pm 01:48 PM

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.

Navicat Connection Timeout: How to Resolve Navicat Connection Timeout: How to Resolve Apr 08, 2025 pm 11:03 PM

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.

How to add a new column in SQL How to add a new column in SQL Apr 09, 2025 pm 02:09 PM

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);

How to view database password in Navicat for MongoDB? How to view database password in Navicat for MongoDB? Apr 08, 2025 pm 09:21 PM

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.

See all articles