Since you mention header files, I'm going to assume you are asking about c++.
Short answer is not without changing the package being depended on.
It's important to distinguish between a truly ROS1 package which uses roscpp etc. and a catkin package which uses catkin for its build process, but is otherwise not linked to ROS.
If the package is actually ROS agnostic, except the build system then its quite easy to make it compile for both ROS1 and ROS2. You need to add conditionals to your package.xml and CMakeLists.txt files
Package.xml
<?xml version="1.0"?>
<package format="3"> <!-- Must use package format 3 -->
<name>my_package</name>
<version>1.0.0</version>
<buildtool_depend condition="$ROS_VERSION == 1">catkin</buildtool_depend>
<buildtool_depend condition="$ROS_VERSION == 2">ament_cmake_auto</buildtool_depend>
<export>
<build_type condition="$ROS_VERSION == 1">catkin</build_type>
<build_type condition="$ROS_VERSION == 2">ament_cmake</build_type>
</export>
</package>
CMakeList.txt
cmake_minimum_required(VERSION 3.5)
project(my_package)
# Verify ROS installation and get ROS version
find_package(ros_environment REQUIRED)
set(ROS_VERSION $ENV{ROS_VERSION})
if(${ROS_VERSION} EQUAL 1)
# Add my ROS1 build commands here
else() #ROS2
# Add my ROS2 build instructions from ament_cmake here
endif()
NOTE: The above approach can also work for message generation if your ROS1 messages conform to the requirements of ROS2 (fields all snake_case etc.).
However, if your package uses ROS libraries such as roscpp, then creating a version that compiles in both cases becomes much more challenging and I would advise creating a separate ROS2 version and or refactoring to allow for some sort of dependency injection of the middleware functions you need. If that's not an option, then you can use precompile flags in your code which is very ugly but will get you what you want. For example this is how you could change some includes:
#if MY_PACKAGE_ROS_VERSION == 2 // ROS2
#include <rclcpp/rclcpp.hpp>
// Do some stuff
#else // ROS1
#include <ros/ros.h>
// Do some stuff
#endif
In that case, you would set the relevant constant in your CMakeLists.txt file with
add_definitions(-DMY_PACKAGE_ROS_VERSION=2)